javascript 此 instanceof 错误消息是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6021245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:10:55  来源:igfitidea点击:

What does this instanceof error message mean?

javascriptinstanceof

提问by pimvdb

I was playing around with instanceofin Chrome but I got an error message. I thinkI know why (you have to supply a function after the instanceofkeyword that is the constructor the object was created with), but the error message seems to be stating something else:

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

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

这是否意味着我应该[1,2,3]用函数替换?我认为这[1,2,3]是正确的,这[]就是问题所在,应该用一个函数替换,但看起来错误消息说的正好相反。

Could someone please explain how I'm interpreting the error message incorrectly?

有人可以解释一下我是如何错误地解释错误消息的吗?

回答by user113716

Objects are instances of a constructor function, so the test is to see if the left hand is an instance ofthe right, so the right must be a function (and it must be the constructor that constructed the object to return true).

对象是构造函数的实例,所以测试是看左边是否是右边的实例,所以右边一定是一个函数(并且它必须是构造要返回的对象的构造函数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

从规范: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.

1.8.6 instanceof 运算符

产生式 RelationalExpression: RelationalExpression instanceof ShiftExpression 的计算方式如下:

  • 让 lref 是评估 RelationalExpression 的结果。
  • 令 lval 为 GetValue(lref)。
  • 让 rref 是评估 ShiftExpression 的结果。
  • 令 rval 为 GetValue(rref)。
  • 如果 Type(rval) 不是 Object,则抛出 TypeError 异常。
  • 如果 rval 没有 [[HasInstance]] 内部方法,则抛出 TypeError 异常。
  • 返回调用带有参数 lval 的 rval 的 [[HasInstance]] 内部方法的结果。

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

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

15.3.5 Properties of Function Instances

15.3.5 函数实例的属性

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.

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

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

所以它需要一个TypeErrorif 右手没有内部[[HasInstance]]属性,但没有指定措辞。

Firefox 4 gives me a much more sensible error message:

Firefox 4 给了我一个更合理的错误信息:

[1,2,3] instanceof [];

// TypeError: invalid 'instanceof' operand []