【发布时间】:2018-06-01 13:55:39
【问题描述】:
在控制台中,当我定义一个构造函数,然后在事后向它添加一个属性时,运行console.log(myConstructor) 不会泄露该属性。但是,当我运行console.dir(myConstructor) 时,我现在可以看到我添加的属性以及许多其他属性。我可以使用console.log 看到的属性与我看不到的属性有什么区别?是否有可以用来区分这两种属性的特定术语或词汇?
function myConstructor(){
this.sayHi = function(){
console.log("hey")
}
}
myConstructor.boop = "thing"
console.log(myConstructor)
=> ƒ myConstructor(){
this.sayHi = function(){
console.log("hey")
}
}
console.dir(myConstructor)
=> ƒ myConstructor()
boop : "thing"
arguments:null
caller:null
length:0
name:"myConstructor"
prototype:{constructor: ƒ}
__proto__:ƒ ()
[[FunctionLocation]]:VM123:1
[[Scopes]]:Scopes[1]
【问题讨论】:
-
这取决于浏览器/js环境如何实现
console。所有浏览器的日志记录行为或多或少不同。即使在浏览器版本之间,也可能存在差异。 -
@Nope 是的,我有。我不是在问这两种方法之间的区别。我在问 console.log 显示的属性与不显示的属性有什么区别。 MDN 文档说
console.log prints the element in an HTML-like tree while console.dir prints the element in a JSON-like tree.这没有回答我的问题。
标签: javascript google-chrome-console browser-console