原型链

//构造函数
function Fn (name,age){
  this.name = name;  
}

//显示原型
Fn.prototype.alertName = function(){
  alert(this.name);
}

//创建实力
var f = new Fn('clm');
f.printName = function(){
  console.log(this.name);
}

f.printName();// clm
f.alertName();// clm
//要去 f.__proto__.__proto__中查找
f.toString();

 

当执行f.toString()时,首先发现 f 中没有这个toString(),这时会去自身的  f.__proto__(隐式原型)中查找,f.__proto__(隐式原型)就是 f 的构造函数的 prototype(显示原型),也就是 Fn.prototype 中去查找, 然后发现 Fn.prototype 中并没有 toString 。

但是 Fn.prototype 也是对象 (普通对象) ,这时会继续向 Fn.__proto__ (隐式原型)就是 Object 的构造函数的 prototype (显示原型)中查找。最后找到 toString

posted @ 2018-02-28 15:34  CLM1010  阅读(96)  评论(0编辑  收藏  举报