理解JS原型链

76 阅读1分钟

代码讲解

<!-- 构造函数 -->
function Person(){}

<!-- 实例 -->
var p1=new Person();

<!-- 实例的__proto__全等于构造函数的prototype -->
console.log(p1.__proto__ === Person.prototype) // true

<!-- 构造函数的prototype的constructor指回构造函数 -->
console.log(Person.prototype.constructor === Person) // true

console.log(p1.__proto__.__proto__) // 指向 Object.prototype

console.log(p1.__proto__.__proto__.__proto__) // 指向 null

配合画图理解

原型链.png

另外,对象有__proto__隐式属性,函数才有prototype原型