原型链

原型:为其他函数提供共享属性和方法的对象。

原型链依赖于__proto__(隐式原型),而非prototype(显式原型)。

   var fun = new Function() = function(){ }

   fun的__proto__属性指向的是其构造函数的原型,fun的构造函数就是Function

   fun.__proto__ = Function.prototype

   例:

       function Fun(); //构造函数Fun

       var f1 = new Fun(){}

       对象f1的原型链:f1.__proto__----->Fun.prototype.__proto__----->Object.prototype.__proto__----->null

每个对象都有__proto__属性,但是只有函数对象才有prototype属性;

原型对象f1.prototype是构造函数Fun的一个实例;

所有函数对象的__proto__都指向Function.prototype,它是一个空函数;

所有对象的__proto__都指向其构造函数的prototype;

例题:

var person1 = new Person();

person1.__proto__ 是什么?

Person.__proto__ 是什么?

Person.prototype.__proto__ 是什么?

Object.__proto__ 是什么?

Object.prototype__proto__ 是什么?

答案:
第一题:
因为 person1.__proto__ === person1 的构造函数.prototype
因为 person1的构造函数 === Person
所以 person1.__proto__ === Person.prototype

第二题:
因为 Person.__proto__ === Person的构造函数.prototype
因为 Person的构造函数 === Function
所以 Person.__proto__ === Function.prototype

第三题:
Person.prototype 是一个普通对象,我们无需关注它有哪些属性,只要记住它是一个普通对象。
因为一个普通对象的构造函数 === Object
所以 Person.prototype.__proto__ === Object.prototype

第四题,参照第二题,因为 Person 和 Object 一样都是构造函数

第五题:
Object.prototype 对象也有proto属性,但它比较特殊,为 null 。因为 null 处于原型链的顶端。
Object.prototype.__proto__ === null

原文地址:https://www.cnblogs.com/hmycheryl/p/8601891.html