Solution Review: Check Instance of Variables

This lesson will explain the solution to the problem in the previous lesson.

Solution #

Press + to interact
var tempFunc1 = function () {
return {}
}
function check(){
var object1 = new tempFunc1();
var object2 = tempFunc1();
return (object1 instanceof tempFunc1 === object2 instanceof tempFunc1);
}
console.log(check())

Explanation #

This question is tricky as it tests your understanding of functions, constructor functions, and the instanceof operator.

Let’s start by understanding the original code first.

Press + to interact
var tempFunc1 = function () {
}
function check(){
var object1 = new tempFunc1();
var object2 = tempFunc1();
return (object1 instanceof tempFunc1 === object2 instanceof tempFunc1);
}
console.log(check())

The code has a function, tempFunc1, which has an empty body. The check function creates two variables, object1 and object, both storing different outputs from tempFunc1. How come?

On line 5, we call tempFunc1 as a constructor ...

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.