JavaScript,原型链顶端为null,如何用代码证明?
JavaScript, the top of the prototype chain is null, how to prove it by code?
Javascript 检查 object.prototype
、object.prototype.prototype
上的 属性,依此类推,直到它命中 null
。到那时,Javascript returns undefined
.
但是如何证明原型链的顶端是null
而不是undefined
呢?
用while循环迭代,用Object#getPrototypeOf得到当前对象的原型:
let object = {};
while(object = Object.getPrototypeOf(object)) {}
console.log(object);
Javascript 检查 object.prototype
、object.prototype.prototype
上的 属性,依此类推,直到它命中 null
。到那时,Javascript returns undefined
.
但是如何证明原型链的顶端是null
而不是undefined
呢?
用while循环迭代,用Object#getPrototypeOf得到当前对象的原型:
let object = {};
while(object = Object.getPrototypeOf(object)) {}
console.log(object);