首先,這裏是按預期工作的例子:爲什麼對象中的屬性是未定義的,雖然它存在於對象的__proto__中?
let a = { foo: 10 }
let b = { bar: 20 }
a.__proto__ = b
// returns value from prototype just fine
a.bar; // 20
這裏是這是個問題,因爲預期不工作下的例子。爲什麼?
// "a" has no prototype when created this way
let a = Object.create(null);
// "b" has prototype and it is a JS native Object with a whole slew of its native props
let b = {};
// assign "a" a prototype
a.__proto__ = b;
// let's try
a.toString; // undefined
// but...
a.__proto__ .toString; // function toString() { [native code] }
爲什麼a.toString
回報undefined
雖然與財產原型分配給它?
a仍然未定義w ith原型參考b。所以toString應該返回undefined – binariedMe