1.

What Is The Instanceof Operator In Javascript? What Would Be The Output Of The Code Below? function Foo() { return Foo; } new Foo() Instanceof Foo;

Answer»

Here, instanceof operator checks the current object and returns true if the object is of the specified TYPE.

For Example:
var DOG = new Animal();

dog instanceof Animal // Output : true

Here dog instanceof Animal is true since dog inherits from Animal.prototype.

 var NAME = new String("xyz");

 name instanceof String // Output : true

Here name instanceof String is true since dog inherits from String.prototype. Now let's understand the code below:

function foo()

return foo; 
}


new foo() instanceof foo;

Here function foo is returning foo, which again POINTS to function foo.

function foo()

return foo; 
}
var BAR = new foo();



// here bar is pointer to function foo(){return foo}.

So the new foo() instanceof foo return false;

Here, instanceof operator checks the current object and returns true if the object is of the specified type.

For Example:
var dog = new Animal();

dog instanceof Animal // Output : true

Here dog instanceof Animal is true since dog inherits from Animal.prototype.

 var name = new String("xyz");

 name instanceof String // Output : true

Here name instanceof String is true since dog inherits from String.prototype. Now let's understand the code below:

function foo()

return foo; 
}


new foo() instanceof foo;

Here function foo is returning foo, which again points to function foo.

function foo()

return foo; 
}
var bar = new foo();



// here bar is pointer to function foo(){return foo}.

So the new foo() instanceof foo return false;



Discussion

No Comment Found