instanceof的实质

 1 <!doctype html>
 2 <html lang="en">
 3  <head>
 4   <meta charset="UTF-8">
 5   <meta name="Generator" content="EditPlus®">
 6   <meta name="Author" content="">
 7   <meta name="Keywords" content="">
 8   <meta name="Description" content="">
 9   <title>Document</title>
10  </head>
11  <body>
12   <script>
13     if(!Object.create){
14         Object.create = function(o){
15             function F(){};
16             F.prototype = o;
17             return new F();
18         }
19     }
20 
21     function A(){};function B(){};function C(){};
22     A.prototype = {};
23     B.prototype = Object.create(A.prototype); 
24     C.prototype = Object.create(B.prototype);
25     var c = new C();
26     alert(c instanceof C);//true
27     alert(c instanceof B);//true
28     alert(c instanceof A);//true
29   </script>
30  </body>
31 </html>

一直以为javascript 的instanceof 是根据function.prototype.contructor找到父类,但是上面的代码就证明我错了。

根据我的推理,javascript的instanceof应该是这样做的

c instanceof A的判断过程如下

for(;;){

  var __proto__ = c.__proto__;

      if( A.prototype === __proto__) return true;

     __proto__  = __proto__.__proto__;

     if(null === __proto___) return false;

}

就是依赖原型链来做的,越来越觉得那个prototype对象里的constructor对象是鸡肋。原型链NICE!

转载于:https://www.cnblogs.com/liguocai/p/3304985.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值