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

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

问题描述

我想添加一个阵列到另一个的元素,所以我在我们可爱的萤火虫尝试这样简单的一句话:

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:


  1. 未定义


  2. 布尔

  3. 编号

  4. 字符串

  5. 对象

请注意,虽然的typeof 有点混乱返回 对象和函数的可调用对象,空实际上不是一个对象和严格来说,在规格符合要求的Jav​​aScript实现所有的功能都被认为是对象。

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 | 

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

注意::由 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.

要观看概述表是如何生成的访问 http://jsfiddle.net/1obxuc7m/

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

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

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