[英]Different console.log()s in stackoverflow snippet vs. browser console - why?
const func = function() { this.name = 'mon' } let f1 = new func let f2 = Object.create(f1) let f3 = Object.create(f2) // The following comments are what the browser console logs: console.log(f1.__proto__) // {constructor: f} console.log(f2.__proto__) // func {name: "mon"} console.log(f3.__proto__) // func {} (here's where this snippet logs func {"name": "mon"})
Also, is an object's 'type' in JS determined by its 'nearest' constructor?另外,JS 中对象的“类型”是否由其“最近的”构造函数决定? (ie func being the type the browser logs for
f1
& f2
)? (即 func 是浏览器为
f1
和f2
记录的类型)?
The difference is that the Stack Snippet console (whose code can be found here ) iterates over properties using for..in
.不同之处在于 Stack Snippet 控制台(可在此处找到其代码)使用
for..in
迭代属性。 This will include enumerable properties anywhere on the prototype chain.这将包括原型链上任何地方的可枚举属性。 This is (part of) how the snippet console figures out properties to log:
这是(部分)代码段控制台如何计算要记录的属性:
function n(e) {
var n = [];
for (var o in e)
n.push(o);
return n
}
For example:例如:
const proto = { prop: 'val' }; const obj = Object.create(proto); console.log(obj); // browser console shows `Object`, but no `prop` property
In contrast, in the browser console, only properties directly on a logged object will be displayed.相比之下,在浏览器控制台中,只会显示直接在记录对象上的属性。 In order to access the internal prototype of the logged object (and see possible properties on the prototype), you'll have to click on the
__proto__
property to expand it.为了访问记录对象的内部原型(并查看原型上可能的属性),您必须单击
__proto__
属性以展开它。
Because the constructor
property of a someFunction.prototype
is not enumerable, it won't be iterated over via for..in
, so it's seen in the browser console, but not in the snippet console.因为
someFunction.prototype
的constructor
属性是不可枚举的,它不会通过for..in
迭代,所以它可以在浏览器控制台中看到,但在代码段控制台中看不到。
const func = function() { this.name = 'mon' } console.log(func.prototype.hasOwnProperty('constructor')); for (const prop in func.prototype) { console.log(prop); // Nothing is logged }
Also, is an object's 'type' in JS determined by its 'nearest' constructor?
另外,JS 中对象的“类型”是否由其“最近的”构造函数决定? (ie func being the type the browser logs for f1 & f2 )?
(即 func 是浏览器为 f1 和 f2 记录的类型)?
Every object has exactly one internal prototype.每个对象都只有一个内部原型。 In your snippet, the internal prototype of
f3
is f2
, the internal prototype of f2
is f1
, and the internal prototype of f1
is func.prototype
.在你的代码段,内部原型
f3
是f2
,内部原型f2
是f1
,并且内部原型f1
是func.prototype
。 Something in a prototype chain may also have a constructor
property ( func.prototype.constructor === func
), but the fact that such a property exists doesn't really have an effect on the internal prototype chain, it's just an convenient method to link a .prototype
object to its associated function
.原型链中的某些东西也可能有一个
constructor
属性( func.prototype.constructor === func
),但是这样一个属性存在的事实对内部原型链没有真正的影响,它只是一个方便的方法将.prototype
对象链接到其关联的function
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.