Solution Review: Check Instance of Variables
Learn how to evaluate whether variables are instances of a function using the instanceof operator in JavaScript. Understand the difference between calling a function as a constructor with new and as a regular function, how returning objects affects instance identity, and how prototype chains influence instanceof results. This lesson clarifies these concepts through code examples and solutions, improving your grasp of object construction and type checking.
We'll cover the following...
Solution #
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.
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 function by calling it with the ...