Javascript checks for the property on object.prototype
, object.prototype.prototype
, and so on until it hits null
. At that point, Javascript returns undefined
.
But how to prove the top of prototype chain is null
rather than undefined
?
Iterate with a while loop, and use Object#getPrototypeOf to get the prototype of the current object:
let object = {};
while(object = Object.getPrototypeOf(object)) {}
console.log(object);