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

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

问题描述

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

[1,2] + [3,4]

它的回应是:

"1,23,4"

这是怎么回事?

解决方案

+ 运算符未为数组定义.

发生的事情是 Javascript 将数组转换为字符串并将它们连接起来.

更新

由于这个问题以及我的回答引起了很多关注,我觉得对 + 运算符的一般行为进行概述会很有用且相关还有.

那么,就这样吧.

不包括 E4X 和实现特定的东西,Javascript(从 ES5 开始)有 6 内置 数据类型:

  1. 未定义
  2. 布尔值
  3. 数量
  4. 字符串
  5. 对象

请注意,虽然 typeof 有些令人困惑地返回 object 用于 Null 和 function 用于可调用对象,Null 实际上不是对象,严格来说,在符合规范的 Javascript 实现中,所有函数都被认为是对象.

没错 - Javascript没有原始数组;只有一个名为 Array 的对象的实例和一些语法糖来减轻痛苦.

更令人困惑的是,包装实体,例如 new Number(5)new Boolean(true)new String("abc") 都是 object 类型,而不是人们想象的数字、布尔值或字符串.然而对于算术运算符 NumberBoolean 的行为与数字相同.

简单吧?完成所有这些之后,我们可以转到概览本身.

+ 按操作数类型的不同结果类型

 ||未定义 |空|布尔值 |数量 |字符串 |对象 |==========================================================================未定义 ||数量 |数量 |数量 |数量 |字符串 |字符串 |空 ||数量 |数量 |数量 |数量 |字符串 |字符串 |布尔值 ||数量 |数量 |数量 |数量 |字符串 |字符串 |数 ||数量 |数量 |数量 |数量 |字符串 |字符串 |字符串 ||字符串 |字符串 |字符串 |字符串 |字符串 |字符串 |对象 ||字符串 |字符串 |字符串 |字符串 |字符串 |字符串 |

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

注意:正如 CMS 所指出的,对于某些情况,例如 NumberBoolean 和自定义的 + 运算符不一定会产生字符串结果.它可以根据对象到原始转换的实现而变化.例如 var o = { valueOf:function () { return 4;} }; 计算 o + 2; 产生 6,一个 number,计算 o + '2' 产生 '42',一个 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] = “1,23,4"?在 JavaScript 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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