为什么可以检索到原型,但 __proto__ 在 JavaScript 中未定义?
Why the prototype can be retrieved but the __proto__ is undefined in JavaScript?
现在正在学习JavaScriptprototype
和__proto__
,找到几个有用的链接
__proto__ VS. prototype in JavaScript
How does __proto__ differ from constructor.prototype?
我可以在 Chrome.
下的以下代码中获取对象 f
的 __proto__
的值
var Foo = function() {}
var f = new Foo();
f.__proto__
> Foo {}
但是,将Foo.prototype.__proto__
设置为null
后,__proto__
的值为undefined
。
var Foo = function() {}
Foo.prototype = {name: 'cat', age: 10};
Foo.prototype.__proto__ = null;
var f = new Foo();
f.__proto__
> undefined
但是我可以得到f.name
的值,也就是cat
。这是我的理解,因为值f.name
是可以检索的,所以对象f
的__proto__
应该指向Foo.prototype
。为什么f.__proto__
的值是undefined
?
__proto__
是 JavaScript 的内部特殊 属性。你不应该使用。
来自mdn
While Object.prototype.proto is supported today in most browsers, its existence and exact behavior has only been standardized in the ECMAScript 6 specification as a legacy feature to ensure compatibility for web browsers. For better support, it is recommended that only Object.getPrototypeOf() be used instead.
According to the ES2015 spec、__proto__
是一个访问器 属性,它 继承自 Object.prototype
。
由于实例 f
的原型链植根于 null
,而不是 Object.prototype
,因此 f
对象不继承 [=11] 的任何属性=],包括 Object.prototype.__proto__
.
对象在内部仍然知道它的原型(通过 [[Prototype]] 内部槽),但它不继承 __proto__
访问器 属性 来获取这个值。不过,您仍然可以通过 Object.getPrototypeOf(f)
访问它。
另请参阅有关 Chromium 问题“obj.__proto__
is undefined if prototype chain does not contain Object.prototype
”的解决方案:
This is working as specified. ES6 __proto__ is a getter defined on Object.prototype. For an object that doesn't have that in its prototype chain it is not accessible (just like, say, hasOwnProperty isn't). You need to use Object.getPrototypeOf instead.
现在正在学习JavaScriptprototype
和__proto__
,找到几个有用的链接
__proto__ VS. prototype in JavaScript
How does __proto__ differ from constructor.prototype?
我可以在 Chrome.
下的以下代码中获取对象f
的 __proto__
的值
var Foo = function() {}
var f = new Foo();
f.__proto__
> Foo {}
但是,将Foo.prototype.__proto__
设置为null
后,__proto__
的值为undefined
。
var Foo = function() {}
Foo.prototype = {name: 'cat', age: 10};
Foo.prototype.__proto__ = null;
var f = new Foo();
f.__proto__
> undefined
但是我可以得到f.name
的值,也就是cat
。这是我的理解,因为值f.name
是可以检索的,所以对象f
的__proto__
应该指向Foo.prototype
。为什么f.__proto__
的值是undefined
?
__proto__
是 JavaScript 的内部特殊 属性。你不应该使用。
来自mdn
While Object.prototype.proto is supported today in most browsers, its existence and exact behavior has only been standardized in the ECMAScript 6 specification as a legacy feature to ensure compatibility for web browsers. For better support, it is recommended that only Object.getPrototypeOf() be used instead.
According to the ES2015 spec、__proto__
是一个访问器 属性,它 继承自 Object.prototype
。
由于实例 f
的原型链植根于 null
,而不是 Object.prototype
,因此 f
对象不继承 [=11] 的任何属性=],包括 Object.prototype.__proto__
.
对象在内部仍然知道它的原型(通过 [[Prototype]] 内部槽),但它不继承 __proto__
访问器 属性 来获取这个值。不过,您仍然可以通过 Object.getPrototypeOf(f)
访问它。
另请参阅有关 Chromium 问题“obj.__proto__
is undefined if prototype chain does not contain Object.prototype
”的解决方案:
This is working as specified. ES6 __proto__ is a getter defined on Object.prototype. For an object that doesn't have that in its prototype chain it is not accessible (just like, say, hasOwnProperty isn't). You need to use Object.getPrototypeOf instead.