原型原型链

77 阅读1分钟

原型

1.所有引用类型都有一个__proto__(隐式原型)属性,属性值是一个普通的对象 
2.所有函数都有一个prototype(原型)属性,属性值是一个普通的对象 
3.所有引用类型的__proto__属性指向它构造函数的prototype
var a = [1,2,3];
a.__proto__ === Array.prototype; // true

原型链

function Parent(month){
    this.month = month;
}

var child = new Parent('Ann');

console.log(child.month); // Ann

console.log(child.father); // undefined

在child中查找某个属性时,会执行下面步骤 访问链路为

1.一直往上层查找,直到到null还没有找到,则返回undefined 
2.Object.prototype.__proto__ === null 
3.所有从原型或更高级原型中的得到、执行的方法,其中的this在执行时,指向当前这个触发事件执行的对象