JavaScript操作符instanceof揭秘

郭文睿 2011-04-02

function ClassA()
{
    this.ma = 'ClassA';
}
ClassA.prototype.fa = function(){return 'prototype function';};

function ClassB()
{
    this.mb = 'ClassB';
}
ClassB.prototype = ClassA.prototype;

var obja = new ClassA();
alert(('ma' in obja) + ' ' + obja.hasOwnProperty('ma')); //output true true
alert(('mb' in obja) + ' ' + obja.hasOwnProperty('mb')); //output false false
alert(('fa' in obja) + ' ' + obja.hasOwnProperty('fa')); //output true false
alert(('fb' in obja) + ' ' + obja.hasOwnProperty('fb')); //output true false
alert(obja instanceof ClassA);   //output true
alert(obja instanceof ClassB);   //output true

var objb = new ClassB();
alert(('ma' in objb) + ' ' + objb.hasOwnProperty('ma')); //output false false
alert(('mb' in objb) + ' ' + objb.hasOwnProperty('mb')); //output true true
alert(('fa' in objb) + ' ' + objb.hasOwnProperty('fa')); //output true false
alert(('fb' in objb) + ' ' + objb.hasOwnProperty('fb')); //output true false
alert(objb instanceof ClassA);   //output true
alert(objb instanceof ClassB);   //output true

      ClassA的prototype与ClassB的prototype是同一个对象,所以给ClassA增加原型方法fa会影响到ClassB,给ClassB增加的原型方法fb也会影响到ClassA,所以obja与objb都拥有属性fa与fb,这一点也比较容易理解。oba没有ClassB的实例属性mb但却是ClassB的实例,objb也没有ClassA的实例属性ma但却是ClassA的实例,这说明instanceof的判断与实例属性无关。既然instanceof与实例属性无关,那么它就跟原型属性有关。事实上,JavaScript根据原型判定instanceof的运算结果。(斑头雁原创:http://bantouyan.iteye.com)

function ClassA()
{
    this.ma = 'ClassA';
}
ClassA.prototype.fa = function(){return 'prototype function fa';};

function ClassB()
{
    this.mb = 'ClassB';
}
ClassB.prototype = ClassA.prototype;
ClassB.prototype.fb = function(){return 'prototype function fb';};

function ClassC()
{
    this.mc = 'ClassC';
}
ClassC.prototype = new ClassB();
ClassC.prototype.fc = function(){return 'prototype function fc';};

var objc = new ClassC();
alert(objc instanceof ClassB);   //output true
alert(objc instanceof ClassA);   //output true
alert(('ma' in objc) + ' ' + objc.hasOwnProperty('ma')); //output false false
alert(('mb' in objc) + ' ' + objc.hasOwnProperty('mb')); //output true false
alert(('mc' in objc) + ' ' + objc.hasOwnProperty('mc')); //output true true
alert(('fa' in objc) + ' ' + objc.hasOwnProperty('fa')); //output true false
alert(('fb' in objc) + ' ' + objc.hasOwnProperty('fb')); //output true false
alert(('fc' in objc) + ' ' + objc.hasOwnProperty('fc')); //output true false
function ClassA()
{
    this.ma = 'ClassA';
}
ClassA.prototype.fa = function(){return 'prototype function fa';};

function ClassB()
{
    this.mb = 'ClassB';
}
ClassB.prototype = ClassA.prototype;
ClassB.prototype.fb = function(){return 'prototype function fb';};

function ClassC()
{
    this.mc = 'ClassC';
}
ClassC.prototype = new ClassB();
ClassC.prototype.fc = function(){return 'prototype function fc';};

function ClassD()
{
    this.md = 'ClassD';
}
ClassD.prototype = ClassC.prototype;
ClassC.prototype.fd = function(){return 'prototype function fd';};

function ClassE()
{
    this.me = 'ClassE';
}
ClassE.prototype = new ClassD();
ClassE.prototype.fe = function(){return 'prototype function fe';};

var obje = new ClassE();
alert(obje instanceof ClassA);   //output true
alert(obje instanceof ClassB);   //output true
alert(obje instanceof ClassC);   //output true
alert(obje instanceof ClassD);   //output true
alert(obje instanceof ClassE);   //output true

alert(('ma' in obje) + ' ' + obje.hasOwnProperty('ma')); //output false false
alert(('mb' in obje) + ' ' + obje.hasOwnProperty('mb')); //output true false
alert(('mc' in obje) + ' ' + obje.hasOwnProperty('mc')); //output false false
alert(('md' in obje) + ' ' + obje.hasOwnProperty('md')); //output true false
alert(('me' in obje) + ' ' + obje.hasOwnProperty('me')); //output true true

alert(('fa' in obje) + ' ' + obje.hasOwnProperty('fa')); //output true false
alert(('fb' in obje) + ' ' + obje.hasOwnProperty('fb')); //output true false
alert(('fc' in obje) + ' ' + obje.hasOwnProperty('fc')); //output true false
alert(('fd' in obje) + ' ' + obje.hasOwnProperty('fd')); //output true false
alert(('fe' in obje) + ' ' + obje.hasOwnProperty('fe')); //output true false

      综合前面的论述与验证,我们可以得出结论,如果obj instanceof Class返回true,那么Class的原型与obj原型链上的某个原型是同一个对象,但这并不意味着obj拥有Class的所有实例属性(但肯定拥有Class的所有原型属性)。(斑头雁原创:http://bantouyan.iteye.com)

相关推荐