Why a property in an object is undefined, although it exists in object's __proto__?

Viewed 122

First, here is an example that works as expected:

let a = { foo: 10 }
let b = { bar: 20 }
a.__proto__ = b

// returns value from prototype just fine
a.bar; // 20

And here is an example that is under the question, that does not work as expected. Why?

// "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] }

Why a.toString returns undefined although a prototype with that property was assigned to it?

1 Answers
Related