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!