这个instanceof错误消息是什么意思? [英] What does this instanceof error message mean?

查看:111
本文介绍了这个instanceof错误消息是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Chrome中玩 instanceof ,但收到了错误消息。我想想我知道为什么(你必须在 instanceof 关键字之后提供一个函数,该关键字是创建对象的构造函数),但错误消息似乎在说明其他内容:

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

这是否意味着我应该用函数替换 [1,2,3] ?我认为 [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?

推荐答案

对象是构造函数的实例,所以测试是看左手是实例是的,所以右边必须是一个函数(它必须是构造对象的构造函数,以返回 true )。

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

来自规范: http://ecma262-5.com/ELS5_HTML.htm#Section_11.8.6


1.8.6 instanceof运算符

ShiftExpression的生产RelationalExpression:RelationalExpression实例的计算方法如下:

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


  • 让lref成为评估RelationalExpression的结果。

  • 让lval为GetValue(lref)。

  • 让rref成为评估ShiftExpression的结果。

  • 让rval为GetValue(rref)。

  • 如果Type(rval)不是Object,则抛出TypeError异常。

  • 如果rval没有[[HasInstance]]内部方法,则抛出TypeErro例外。

  • 使用参数lval返回调用[[HasInstance]] rval内部方法的结果。

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

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

15.3.5函数实例的属性


除了必需的内部属性外,每个函数实例都有[[Call]]内部属性,并且在大多数情况下使用不同的版本[[Get]]内部财产。 根据它们的创建方式(见8.6.2,13.2,15和15.3.4.5),函数实例可能有[[HasInstance]]内部属性,[[Scope]]内部属性,[[[Construct]]内部属性,[[FormalParameters]]内部属性,[[Code]]内部属性,[[TargetFunction]]内部属性,[[BoundThis]]内部属性和[[ BoundArgs]]内部属性。

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.

所以如果右手没有内部,则需要 TypeError [[HasInstance]] 属性,但未指定措辞。

So it requires a TypeError if the right hand does not have an internal [[HasInstance]] property, but doesn't specify the wording.

Firefox 4给了我更多明智的错误信息:

Firefox 4 gives me a much more sensible error message:

[1,2,3] instanceof [];

// TypeError: invalid 'instanceof' operand []

这篇关于这个instanceof错误消息是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆