为什么__proto__未定义? [英] Why is __proto__ undefined?

查看:452
本文介绍了为什么__proto__未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Javascript的原型时,我遇到了这种行为,我无法解释。我在chrome的控制台(即V8)中测试它。

While reading on Javascript's prototypes I encountered this behaviour I can't explain. I am testing this in chrome's console (which is V8).

var fruit = {taste:'good'};
var banana = Object.create(fruit);
console.log(banana.taste); //"good"
console.log(banana.__proto__); //Object {taste: "good"}
console.log(Object.getPrototypeOf(banana)); //Object {taste: "good"}

到目前为止一切都符合预期。
但是,如果我这样做:

So far everything is as expected. However if I do this:

var drink = Object.create(null);
Object.defineProperty(drink, 'taste', {value:"nice"});
var coke = Object.create(drink);
console.log(coke.taste); //"nice"
console.log(coke.__proto__); //undefined
console.log(Object.getPrototypeOf(coke)); //Object {taste: "nice"}

然后可乐.__ proto__ = == undefined 。为什么在第二种情况下 undefined

then coke.__proto__ === undefined. Why is it undefined in the second case?

推荐答案

我曾经< a href =https://code.google.com/p/chromium/issues/detail?id=506166\"rel =nofollow>为此行为打开了一个问题,但它已作为标准关闭 - 合规行为。根据该问题的接近原因:

I once opened an issue for this behavior, but it was closed as standards-compliant behavior. According to the issue's close reason:


这是按指定的方式工作。 ES6 __ proto __ 是在Object.prototype上定义的getter。对于在其原型链中没有该对象的对象,它是不可访问的(就像,例如,hasOwnProperty不是)。你需要使用Object.getPrototypeOf。

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.

确实如此: ES6部分B.2.2.1 定义 Object.prototype。 __proto __ ;因此, __ proto __ 属性继承自 Object.prototype 。但是,您的可乐对象是使用 Object.create(null)创建的,因此它没有 Object.prototype 在其原型链中。

This is indeed true: ES6 section B.2.2.1 defines Object.prototype.__proto__; therefore, the __proto__ property is inherited from Object.prototype. However, your coke object was created with Object.create(null), so it doesn't have Object.prototype in its prototype chain.

对象始终具有其原型的内部知识,存储在其 [[Prototype]]内部插槽 __ proto __ 属性是一种通过代码访问内部已知原型的方法。对象缺少 __ proto __ 属性不会影响其[[Prototype]]插槽,该插槽仍然存在。

An object always has internal knowledge to its prototype, stored in its [[Prototype]] internal slot. The __proto__ property is a way to access that internally-known prototype through code. An object's lack of a __proto__ property does not affect its [[Prototype]] slot, which still exists.

非常清楚:可乐 有原型(存储在[[Prototype]]中),原型是对象。你可以用 Object.getPrototypeOf(可乐)看到这个。但是,这就是整个原型链,因为 drink 的原型是 null 。因此, coke 无法从 Object.prototype .__ proto __ __ proto __ $ c>因为它的原型链中没有 Object.prototype

To be perfectly clear: coke does have a prototype (stored in [[Prototype]]), and that prototype is the object drink. You can see this with Object.getPrototypeOf(coke). However, that is the entire prototype chain, because the prototype of drink is null. Therefore, coke can't inherit __proto__ from Object.prototype.__proto__ because it doesn't have Object.prototype in its prototype chain.

这篇关于为什么__proto__未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆