问题描述
我想添加一个阵列到另一个的元素,所以我在我们可爱的萤火虫尝试这样简单的一句话:
I wanted to add the elements of an array into another, so I tried this simple sentence in our beloved Firebug:
[1,2] + [3,4]
据回应:
"1,23,4"
这是怎么回事?
推荐答案
的 +
操作符的是不是数组确定。
什么情况是,使用Javascript的数组转换成字符串并并置的。
What happens is that Javascript converts arrays into strings and concatenates those.
 
由于这个问题,因此我的答案是获得了很多的关注,我觉得这将是有益的和相关的有一个概述有关如何在 +
经营者的行为一般也。
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.
所以,在这里它去。
撇除E4X和实现特定的东西,使用Javascript(如ES5的)有 6 内置的的:
Excluding E4X and implementation-specific stuff, Javascript (as of ES5) has 6 built-in data types:
- 未定义
- 空
- 布尔
- 编号
- 字符串
- 对象
请注意,虽然的typeof
的 对象和
函数
的可调用对象,空实际上不是一个对象和严格来说,在规格符合要求的JavaScript实现所有的功能都被认为是对象。
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.
这是正确的 - JavaScript有否基本数组这样;只有被称为对象的实例
阵列
一些语法糖来缓解疼痛。
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.
添加更多的混乱,包装实体,如
新号码(5)
,新布尔值(true)
和新的String(ABC)
都是对象
类型,而不是数字,布尔值或字符串正如人们所预料的。不过对于算术运算符编号
和布尔
表现得像号码。
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 |
注意::由 CMS ,对物体的某些情况下,如
编号
,布尔
和自定义的在 +
运营商不一定会产生一个字符串结果。它可以根据对象的执行原语转换而变化。例如变种O = {的valueOf:函数(){返回4; }};
评估 O + 2;
产生 6
,一个数量
,评估 O +2
产生 1942
,一个字符串
。
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] =" 1,23,4"在JavaScript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!