Search⌘ K
AI Features

Solution Review: Instance of Array?

Explore how to use the 'instanceof' operator to check if an object is an instance of Array in JavaScript. Understand prototype chain modifications by assigning Array.prototype to a constructor's prototype to pass the 'instanceof' test. This lesson clarifies type coercion and prototype inheritance through practical coding examples.

We'll cover the following...

Solution

Javascript (babel-node)
function check(){
var tempFunc = function () {}
tempFunc.prototype = Array.prototype
return new tempFunc instanceof Array;
}
console.log(check())

Explanation

The exercise required you to write some code so that the line

return new tempFunc instanceof Array 

evaluates to true.

Let’s understand the original code first: ...

Javascript (babel-node)
function check(){
var tempFunc = function () {}
return new tempFunc instanceof Array;
}
console.log(check())

In the ...