当前位置: 代码迷 >> 综合 >> js-Higher-base.js
  详细解决方案

js-Higher-base.js

热度:9   发布时间:2024-01-20 05:41:39.0
// 1.基于原型链的继承
// 继承属性
// 当访问一个对象的属性时发生的行为:
// 假定有一个对象 o, 其自身的属性(own properties)有 a 和 b:
{a: 1, b: 2} // 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
// 继承方法

 


更多专业前端知识,请上 【猿2048】www.mk2048.com
  相关解决方案