// 假定有一个对象 o, 其自身的属性(own properties)有 a 和 b: // {a: 1, b: 2} // o 的原型 o.[[Prototype]]有属性 b 和 c: // {b: 3, c: 4} // 最后, o.[[Prototype]].[[Prototype]] 是 null. // 这就是原型链的末尾,即 null, // 根据定义,null 没有[[Prototype]].
// 综上,整个原型链如下: // {a:1, b:2} ---> {b:3, c:4} ---> null console.log(o.a); // 1 // a是o的自身属性吗?是的,该属性的值为1 console.log(o.b); // 2 // b是o的自身属性吗?是的,该属性的值为2 // o.[[Prototype]]
上还有一个'b'属性,但是它不会被访问到.这种情况称为"属性遮蔽 (property shadowing)". console.log(o.c); // 4 // c是o的自身属性吗?不是,那看看o.[[Prototype]]上有没有. // c是o.[[Prototype]]
的自身属性吗?是的,该属性的值为4 console.log(o.d); // undefined // d是o的自身属性吗?不是,那看看o.[[Prototype]]上有没有. // d是o.[[Prototype]]
的自身属性吗?不是,那看看o.[[Prototype]].[[Prototype]]上有没有. // o.[[Prototype]].[[Prototype]]为null,停止搜索, // 没有d属性,返回undefined
原型链
最新推荐文章于 2024-06-12 22:29:50 发布