Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this instanceof error message mean?

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?

like image 260
pimvdb Avatar asked May 16 '11 17:05

pimvdb


People also ask

What does Instanceof mean in JavaScript?

The instanceof operator in JavaScript is used to check the type of an object at run time. It returns a boolean value if true then it indicates that the object is an instance of a particular class and if false then it is not.

What does Instanceof do in typescript?

The Typescript instanceof is one of the operators, and it is used to determine the specific constructor, and it will be creating the object of the classes. It will call the methods with the help of instance like that if we use an interface that can be implemented and extended through the classes.

How do you check if an object is an error in JavaScript?

To check if a variable is of type Error , use the instanceof operator - err instanceof Error . The instanceof operator returns true if the prototype property of a constructor appears in the prototype chain of the object. Copied!

Is a type but is being used as a value?

The 'instanceof' error "only refers to a type, but is being used as a value here" occurs when we try to use the instanceof operator with a type instead of a value. To solve the error, create a class instead, or simply use a user-defined type guard.


1 Answers

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).

[1,2,3] instanceof [].constructor;  // 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

1.8.6 The instanceof operator

The production RelationalExpression: RelationalExpression instanceof ShiftExpression is evaluated as follows:

  • Let lref be the result of evaluating RelationalExpression.
  • Let lval be GetValue(lref).
  • Let rref be the result of evaluating ShiftExpression.
  • Let rval be GetValue(rref).
  • If Type(rval) is not Object, throw a TypeError exception.
  • If rval does not have a [[HasInstance]] internal method, throw a TypeError exception.
  • Return the result of calling the [[HasInstance]] internal method of rval with argument lval.

and http://ecma262-5.com/ELS5_HTML.htm#Section_15.3.5

15.3.5 Properties of Function Instances

In addition to the required internal properties, every function instance has a [[Call]] internal property and in most cases use a different version of the [[Get]] internal property. Depending on how they are created (see 8.6.2 ,13.2, 15, and 15.3.4.5), function instances may have a [[HasInstance]] internal property, a [[Scope]] internal property, a [[Construct]] internal property, a [[FormalParameters]] internal property, a [[Code]] internal property, a [[TargetFunction]] internal property, a [[BoundThis]] internal property, and a [[BoundArgs]] internal property.

So it requires a TypeError if 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:

[1,2,3] instanceof [];

// TypeError: invalid 'instanceof' operand []
like image 84
user113716 Avatar answered Oct 11 '22 11:10

user113716