为什么[1,2] + [3,4] =" 1,23,4"在JavaScript? [英] Why is [1,2] + [3,4] = "1,23,4" in JavaScript?

查看:176
本文介绍了为什么[1,2] + [3,4] =" 1,23,4"在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数组的元素添加到另一个中,所以我尝试了这个:

  [1,2] + [3,4] 

它回复:

 1,23,4

什么是继续?

解决方案

未定义 + 运算符对于数组



Javascript 将数组转换为字符串并将其连接起来会发生什么。



 



更新



由于这个问题因此我的回答得到了很多关注我觉得它会了解 + 运算符的行为概述是否有用且相关。



所以,就这样了。



除了E4X和特定于实现的东西,Javascript(ES5之后)内置 6 数据类型


  1. 未定义

  2. Null

  3. 布尔

  4. 数字

  5. 字符串

  6. 对象

注意虽然 typeof somew对于可调用对象,Null和函数混淆地返回 对象,Null实际上不是对象并严格说来,在符合规范的Javascript实现中,所有函数都被认为是对象。



这是正确的 - Javascript有没有原始数组;只有一个名为 Array 的Object的实例,带有一些语法糖来减轻痛苦。



添加更多的混乱,包装器实体,例如 new Number(5) new Boolean(true) new String (abc)都是对象类型,而不是人们可能期望的数字,布尔值或字符串。然而,对于算术运算符 Number Boolean 表现为数字。



<很容易,对吧?完成所有这些后,我们可以继续进行概述。



+ 按操作数类型

  || undefined | null |布尔值|号码|字符串|对象| 
============================================== ===========================
undefined ||号码|号码|号码|号码|字符串|字符串|
null ||号码|号码|号码|号码|字符串|字符串|
boolean ||号码|号码|号码|号码|字符串|字符串|
number ||号码|号码|号码|号码|字符串|字符串|
string ||字符串|字符串|字符串|字符串|字符串|字符串|
对象||字符串|字符串|字符串|字符串|字符串|字符串|

*适用于Chrome13,FF6,Opera11和IE9。检查其他浏览器和版本留给读者练习。



注意:正如 CMS ,对于某些对象,例如数字 Boolean 和自定义的 + 运算符不一定产生字符串结果。它可以根据对象到原始转换的实现而变化。例如 var o = {valueOf:function(){return 4; } <; code>评估 o + 2; 生成 6 number ,评估 o +'2'生成 '42',a string



要查看概览表的生成方式,请访问 http://jsfiddle.net/1obxuc7m/


I wanted to add the elements of an array into another, so I tried this:

[1,2] + [3,4]

It responded with:

"1,23,4"

What is going on?

解决方案

The + operator is not defined for arrays.

What happens is that Javascript converts arrays into strings and concatenates those.

 

Update

Since this question and consequently my answer is getting a lot of attention I felt it would be useful and relevant to have an overview about how the + operator behaves in general also.

So, here it goes.

Excluding E4X and implementation-specific stuff, Javascript (as of ES5) has 6 built-in data types:

  1. Undefined
  2. Null
  3. Boolean
  4. Number
  5. String
  6. Object

Note that although typeof somewhat confusingly returns object for Null and function for callable Objects, Null is actually not an Object and strictly speaking, in specification-conforming Javascript implementations all functions are considered to be Objects.

That's right - Javascript has no primitive arrays as such; only instances of an Object called Array with some syntactic sugar to ease the pain.

Adding more to the confusion, wrapper entities such as new Number(5), new Boolean(true) and new String("abc") are all of object type, not numbers, booleans or strings as one might expect. Nevertheless for arithmetic operators Number and Boolean behave as numbers.

Easy, huh? With all that out of the way, we can move on to the overview itself.

Different result types of + by operand types

            || undefined | null   | boolean | number | string | object |
=========================================================================
 undefined  || number    | number | number  | number | string | string | 
 null       || number    | number | number  | number | string | string | 
 boolean    || number    | number | number  | number | string | string | 
 number     || number    | number | number  | number | string | string | 
 string     || string    | string | string  | string | string | string | 
 object     || string    | string | string  | string | string | string | 

* applies to Chrome13, FF6, Opera11 and IE9. Checking other browsers and versions is left as an exercise for the reader.

Note: As pointed out by CMS, for certain cases of objects such as Number, Boolean and custom ones the + operator doesn't necessarily produce a string result. It can vary depending on the implementation of object to primitive conversion. For example var o = { valueOf:function () { return 4; } }; evaluating o + 2; produces 6, a number, evaluating o + '2' produces '42', a string.

To see how the overview table was generated visit http://jsfiddle.net/1obxuc7m/

这篇关于为什么[1,2] + [3,4] =&quot; 1,23,4&quot;在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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