Iladarsda
Iladarsda

Reputation: 10696

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

Upvotes: 0

Views: 124

Answers (1)

Jamiec
Jamiec

Reputation: 136104

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

Upvotes: 3

Related Questions