0

I have a simple piece of JavaScript code creating object b using Object.create and passing object a as the argument:

const a = {};
const b = Object.create(a);
console.log(b);

It is my understanding that Object.create will point the __proto__ of b to a when creating this new object.

And I can confirm my assumption by logging some values:

console.log(b.__proto__ === a); // true
console.log(a.__proto__ === Object.prototype); // true
console.log(Object.prototype.__proto__ === null); // true

But the result of logging b in the developer console is somewhat confusing as the value of b.__proto__.__proto__.__proto__ doesn't seem to be null:

enter image description here

Nojan A.
  • 881
  • 1
  • 11
  • 27
  • There are no standards governing how objects should appear when logged to the console. It is up to each implementation to do what they want. The WHATWG published [a Living Standard](https://console.spec.whatwg.org/#logger) of what `console.log` should do in terms of format strings and the like, but still leaves it to implementations to determine how to show object. If you want to talk about a specific implementation, tag the question with that browser. But know you're making assumptions about the internal representations of JavaScript internals based on essentially one group's decisions. – Heretic Monkey Jul 12 '23 at 16:31
  • 1
    Expanding a getter property like `__proto__` in a prototype object actually invokes the getter on the logged value, i.e. it's getting `b.__proto__`, which displays `a` again. Your understanding of the prototype chain is fine. Do not click on `__proto__`, rather scroll down to see the *[[prototype]]* being `null` on `Object.prototype`. – Bergi Jul 12 '23 at 17:21
  • @HereticMonkey I understand your point. But I get the same result in both Firefox and Chrome, therefore I assume I'm misunderstanding something about the prototypal inheritance. – Nojan A. Jul 12 '23 at 17:21

0 Answers0