使用 Object.create(null) 创建对象时 __proto__ 如何工作
How does __proto__ work when object is created with Object.create(null)
考虑以下 javascript 代码
var a = Object.create(null);
a.foo = 1;
var b = Object.create(a);
console.log(b.foo); //prints 1
console.log(b.__proto__); //prints undefined
b.__proto__ = null;
console.log(b.__proto__); //prints null
console.log(b.foo); //prints 1
任何人都可以解释对象 b
如何访问 a
的 "foo" 属性,甚至在将 b.__proto__
设置为 null 之后?用于访问a
的属性的内部link是什么?
我尝试通过 SO 搜索可能的解释,但找不到对 Javascript 的这种特殊行为的任何解释。
您的问题是您正在使用 __proto__
property,它是 Object.prototype
上的 getter/setter - 但您的对象不继承自它,所以它是 undefined
首先,作业会创建一个标准 属性,名称为 __proto__
.
改用正确的 Object.getPrototypeOf
/Object.setPrototypeOf
,代码将达到您的预期:
var a = Object.create(null);
a.foo = 1;
var b = Object.create(a);
console.log(b.foo); // 1
console.log(Object.getPrototypeOf(b)); // {foo:1} - a
Object.setPrototypeOf(b, null);
console.log(Object.getPrototypeOf(b)); // null
console.log(b.foo); // undefined
@Bergi 的回答是正确的。这是 in-depth 回答 __proto__
情况下发生的情况
var a = Object.create({});
var b = Object.create(a);
b.__proto__===a; //true
var c = Object.create(null);
var d = Object.create(c);
d.__proto__===c; //false..confusion
Object.hasOwnProperty.call(d,"__proto__"); //false as expected
Object.hasOwnProperty.call(b,"__proto__"); //false ?
Object.hasOwnProperty.call(Object,"__proto__"); //false
Object.hasOwnProperty.call(Object.prototype,"__proto__"); //true
这意味着 __proto__
仅存在于 Object.prototype
。
Object.getOwnPropertyDescriptor(Object.prototype,"__proto__")
//{enumerable: false, configurable: true, get: ƒ, set: ƒ}
__proto__
是一个 getter setter 应该 return 内部 link 到 object parent 像
get __proto__(){return this.hidden_internal_link_to_parent;}
案例 b.__proto__
:- b
没有 __proto__
属性 所以它通过 [[prototype]]
链到 a
,然后到 a
的 parent,最后到 Object.prototype
。 Object.prototype
有 __proto__
并且它 return 是 b
的 parent 的 link 即 a
.
案例 d.__proto__
:- d
的 link 到 Object.prototype
已损坏(d --parent-->c 和 c-- parent-->空)。所以 d.__proto__
是未定义的。但是 d
有内部 link 到 c
可以通过 Object.getPrototypeOf(d)
.
访问
考虑以下 javascript 代码
var a = Object.create(null);
a.foo = 1;
var b = Object.create(a);
console.log(b.foo); //prints 1
console.log(b.__proto__); //prints undefined
b.__proto__ = null;
console.log(b.__proto__); //prints null
console.log(b.foo); //prints 1
任何人都可以解释对象 b
如何访问 a
的 "foo" 属性,甚至在将 b.__proto__
设置为 null 之后?用于访问a
的属性的内部link是什么?
我尝试通过 SO 搜索可能的解释,但找不到对 Javascript 的这种特殊行为的任何解释。
您的问题是您正在使用 __proto__
property,它是 Object.prototype
上的 getter/setter - 但您的对象不继承自它,所以它是 undefined
首先,作业会创建一个标准 属性,名称为 __proto__
.
改用正确的 Object.getPrototypeOf
/Object.setPrototypeOf
,代码将达到您的预期:
var a = Object.create(null);
a.foo = 1;
var b = Object.create(a);
console.log(b.foo); // 1
console.log(Object.getPrototypeOf(b)); // {foo:1} - a
Object.setPrototypeOf(b, null);
console.log(Object.getPrototypeOf(b)); // null
console.log(b.foo); // undefined
@Bergi 的回答是正确的。这是 in-depth 回答 __proto__
var a = Object.create({});
var b = Object.create(a);
b.__proto__===a; //true
var c = Object.create(null);
var d = Object.create(c);
d.__proto__===c; //false..confusion
Object.hasOwnProperty.call(d,"__proto__"); //false as expected
Object.hasOwnProperty.call(b,"__proto__"); //false ?
Object.hasOwnProperty.call(Object,"__proto__"); //false
Object.hasOwnProperty.call(Object.prototype,"__proto__"); //true
这意味着 __proto__
仅存在于 Object.prototype
。
Object.getOwnPropertyDescriptor(Object.prototype,"__proto__")
//{enumerable: false, configurable: true, get: ƒ, set: ƒ}
__proto__
是一个 getter setter 应该 return 内部 link 到 object parent 像
get __proto__(){return this.hidden_internal_link_to_parent;}
案例 b.__proto__
:- b
没有 __proto__
属性 所以它通过 [[prototype]]
链到 a
,然后到 a
的 parent,最后到 Object.prototype
。 Object.prototype
有 __proto__
并且它 return 是 b
的 parent 的 link 即 a
.
案例 d.__proto__
:- d
的 link 到 Object.prototype
已损坏(d --parent-->c 和 c-- parent-->空)。所以 d.__proto__
是未定义的。但是 d
有内部 link 到 c
可以通过 Object.getPrototypeOf(d)
.