简体   繁体   中英

JavaScript instanceof

Can you please tell my why in the example below sub instanceof Super is false ?

function Super(){
    var obj = {
        prop1: "value1"
    };
    return obj;
}

var sub = new Super();
sub instanceof Super // false

Because its not an instance of that type - you've returned an anonymous object. If you would have written it like this:

function Super(){
 this.prop1 = 'value1';   
}

var sub = new Super();
console.log(sub instanceof Super) // true

It would work as intended

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM