原型链 — instanceof

//构造函数
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();

//instanceof用于判断引用类型属于哪个构造函数的方法
//f instanceof Fn的判断逻辑是:
//f 的 __proto__ 一层一层往上,能否对应到 Fn.prototype
console.log(f instanceof Fn);//true
//再试着判断 f instanceof Object
console.log(f instanceof Object);//true

 

posted @ 2018-02-28 16:46  CLM1010  阅读(133)  评论(0编辑  收藏  举报