Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

javascript - Firefox Javascript“无法转换为原始类型”错误是什么意思?(Firefox Javascript what does “can't convert to a primitive type” error mean?)

this is my javascript test code:

(这是我的javascript测试代码:)

 console.log( [] == 'a' ); 

when running in my firefox it gives this error: "TypeError: can't convert [] to primitive type"

(在我的Firefox中运行时,出现以下错误:“ TypeError:无法将[]转换为原始类型”)

what does this mean?

(这是什么意思?)

It does not seem to apply to every browser/browser version, or in my case, even a tab.

(它似乎并不适用于所有浏览器/浏览器版本,或者就我而言,甚至不适用于选项卡。)

I have two chrome tabs open, if I try the code in one it gives an error, but in the other tab it works fine - making me very confused about the inconsistency.

(我打开了两个Chrome标签,如果我在其中一个尝试了代码,则会出错,但在另一个标签中,它可以正常工作-使我对不一致感到困惑。)

Here is an image showing the errors

(这是显示错误的图像)

So what am I missing here, why the inconsistency on chrome?

(那么,我在这里想念的是什么,为什么chrome不一致?)

Any help appreciated!

(任何帮助表示赞赏!)

(EDIT 1) I've found that the error comes when adding/changing a bunch of prototypes onto the Array object, so how can I add prototypes without causing this error?

((编辑1)我发现在Array对象上添加/更改一堆原型时会出现错误,因此如何添加原型而不引起此错误?)

 { // if I omit the first line, the error does not occur Array.prototype.join = Array.prototype.concat; console.log( [] == 'a' ); } 

  ask by SSJCoder translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As you note, this is a consequence of modifying the Array prototype.

(如您所述,这是修改Array原型的结果。)

Specifically because the method toString is used during an equality check between an array and a primitive value.

(特别是因为在数组和原始值之间的相等性检查期间使用了toString方法。)

Generally, when you use == where one operand is an object, javascript will try to convert the object to a primitive using the steps outlined in the ECMAScript Spec .

(通常,当您使用==其中一个操作数是一个对象),javascript将尝试使用ECMAScript Spec中概述的步骤将对象转换为基本体。)

Parts 3 and 4 specifically:

(第3部分和第4部分专门:)

  1. If hint is "string", then let methodNames be ?"toString", "valueOf"?.

    (如果提示是“字符串”,则将methodNames设为“” toString”,“ valueOf”?。)

  2. Else, Let methodNames be ?"valueOf", "toString"?.

    (否则,让methodNames为?“ valueOf”,“ toString”?。)

As you can see, the toString method will be called as part of the attempt to convert the array to a primitive.

(如您所见,在将数组转换为原语的尝试过程中,将调用toString方法。)

The array valueOf method does not return a primitive by default, and since you've overriden the toString method , now it does not return a primitive either!

(默认情况下,数组valueOf方法不返回基元,并且由于您已经覆盖了toString方法 ,因此它也不返回基元!)

Thus we move past step 5 and go on to 6 which says:

(因此,我们经过步骤5,然后继续执行步骤6,其中说:)

  1. Throw a TypeError exception.

    (引发TypeError异常。)

Here's a demonstration:

(这是一个示范:)

 const oldToString = Array.prototype.toString; Array.prototype.toString = function() { console.log("Array toString method called!"); return "[" + this.join(", ") + "]"; } // No type error, because we converted to a primitive (in this // case, a string) successfully. console.log([1, 2, 3] == "[1, 2, 3]") Array.prototype.toString = function() { return {}; } // Type error, because we tried to convert to a primitive using // toString, but we got an object back?! console.log([1, 2, 3] == "[1, 2, 3]") 

To be clear, this issue has nothing to do with browser differences or Firefox specifically but is instead a consequence of your modifications combined with the ECMAScript spec which all browsers follow.

(明确地说,此问题与浏览器差异或Firefox无关,而是由您的修改与所有浏览器都遵循的ECMAScript规范结合而成的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...