I was playing around with instanceof in Chrome but I got an error message. I think I know why (you have to supply a function after the instanceof keyword that is the constructor the object was created with), but the error message seems to be stating something else:
[1,2,3] instanceof Array
// true
[1,2,3] instanceof []
// TypeError: Expecting a function in instanceof check, but got 1,2,3
Does this mean that I should replace [1,2,3] with a function? I would think that [1,2,3] is correct and that [] is the problem and should be replaced with a function, but it looks like the error message is saying the opposite.
Could someone please explain how I’m interpreting the error message incorrectly?
Objects are instances of a constructor function, so the test is to see if the left hand is an instance of the right, so the right must be a function (and it must be the constructor that constructed the object to return
true).So to answer the question more directly, your initial understanding is correct, and the error message seems misleading (to me anyway).
From the spec: http://ecma262-5.com/ELS5_HTML.htm#Section_11.8.6
and http://ecma262-5.com/ELS5_HTML.htm#Section_15.3.5
15.3.5 Properties of Function Instances
So it requires a
TypeErrorif the right hand does not have an internal[[HasInstance]]property, but doesn’t specify the wording.Firefox 4 gives me a much more sensible error message: