JavaScript Array + Array = String?

JavaScript数组字符串+数组= ?

问题 (Question)

When you try to add an Array to another Array in JavaScript, it turns it into a String. Typically when this operation is performed in another languages, the lists are combined.

JavaScript

[1, 2] + [3, 4] = "1,23,4"

Python

[1, 2] + [3, 4] = [1, 2, 3, 4]

I'm not trying to start a flame war about which language is better. But JavaScript's makes no sense to me. Is there a logical reason for why JavaScript does this? Or is it just a quirk of the language?

当你试图添加一个数组到另一个数组在JavaScript中,它将成一个字符串。通常当执行这个操作在另一语言中,列表相结合。

Javascript

[1, 2] + [3, 4] = "1,23,4"

Python

[1, 2] + [3, 4] = [1, 2, 3, 4]

我不想开始一个火焰战争哪些语言是更好的。但JavaScript的对我毫无意义。有一个合乎逻辑的理由为什么JavaScript这?还是只是一个语言的怪癖?

最佳答案 (Best Answer)

This is how the Addition Operator (+) is defined..

The + operator does not have a "special meaning/overload" for other value types (like arrays) and the behavior in ECMAScript is independent of the behavior in any other language.

The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression is evaluated as follows:

  1. Let lref be the result of evaluating AdditiveExpression.
  2. Let lval be GetValue(lref).
  3. Let rref be the result of evaluating MultiplicativeExpression.
  4. Let rval be GetValue(rref).
  5. Let lprim be ToPrimitive(lval).
  6. Let rprim be ToPrimitive(rval).
  7. If Type(lprim) is String or Type(rprim) is String, then Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
  8. Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim). See the Note below 11.6.3.

The conversion in this case is rather subtle, but is the result of the array values, being Objects without a corresponding primitive type, having "toString" invoked as a result of the [ToPrimitive] operation. As such the rule application is approximately as follows.

   [1,2] + [3,4]
-> [1,2].toString() + [3,4].toString()     // Rules #5 and #6
-> "1,2" + "3,4"                           // Rule #7
-> "1,23,4"

这就是加法操作符(+)定义. .

+运营商没有“特殊意义/过载”为其他值类型(比如数组)和ECMAScript的行为是独立的行为在任何其他语言。

生产AdditiveExpression:AdditiveExpression + MultiplicativeExpression评估如下:

  1. 让lref AdditiveExpression评估的结果。
  2. 让lval GetValue(lref)。
  3. 让rref MultiplicativeExpression评估的结果。
  4. 让rval GetValue(rref)。
  5. 让lprim ToPrimitive(lval)。
  6. 让rprim ToPrimitive(rval)。
  7. 如果类型(lprim)是字符串或类型(rprim)是字符串,然后返回的结果的字符串连接ToString(lprim)其次是ToString(rprim)
  8. 应用加法操作的结果返回当时(lprim)和当时(rprim)。请参见下面的说明11.6.3。

转换在这种情况下是相当微妙,但数组值的结果,被对象没有一个相应的原始类型,调用toString”的结果[ToPrimitive]操作。这样的规则应用程序大致如下。

   [1,2] + [3,4]
-> [1,2].toString() + [3,4].toString()     // Rules #5 and #6
-> "1,2" + "3,4"                           // Rule #7
-> "1,23,4"

答案 (Answer) 2

Yes, JavaScript(JS) has quite a few 'quirks' to put it mildly. To get the behavior you desire, you can use the concat method. [1, 2].concat([3, 4])

The source of the confusion is, as Douglas Crockford says in his excellent book, "JavaScript: The Good Parts" arrays in JS are "Phony Arrays", (they do not do a linear allocation of memory among other things). They are actually objects with Array like characteristics which they inherit from Array.prototype. This is both good and bad, so beware.

In fact he puts JS arrays in the "Awful Parts" of JS section, where all problematic things in JS which cannot be easily avoided, are relegated to.

是的,JavaScript(JS)相当多的“怪癖”说得婉转些。得到你想要的行为,您可以使用concat方法。[1, 2].concat([3, 4])

混乱的根源是道格拉斯Crockford说在他的优秀作品,“JavaScript:好的部分”数组在JS“假的数组”,(他们不做线性分配内存等)。他们实际上是对象数组等特���,他们从Array.prototype继承。这是两个好的和坏的,所以要小心。

事实上他把JS数组在JS的“可怕的”部分,所有有问题的东西在JS难以避免,降级。

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23450642