赞
踩
数值+数值:
1 + 1 = 2;
1 + NaN = NaN;
Infinity + - Infinity = NaN;
Infinity + Infinity = Infinity;
-Infinity + - Infinity = -Infinity;
+0 + +0 = +0;
+0 + -0 = +0;
-0 + -0 = -0;
字符串 + 字符串
'hello ' + 'world' = 'hello world'
直接拼接
字符串 + 数值
'10' + 10.10 = '1010.1'
'1' + 2 + 3 + 4 = '1234'
1 + 2 + 3 + '4' = '64'
1 + (2 + '3') + 4 = '1234'
字符串和数值相加会先从左往右运算,然后把对应数值先转换为字符串,10.10会被转换成’10.1’。加法运算哪怕只有一个字符串,也会把整个运算结果变成字符串
字符串 + 非数值
'1' + NaN = '1NaN'
'1' + Infinity = '1Infinity'
'1' + -Infinity = '1-Infinity'
'1' + true = '1true'
'1' + false = '1false'
'1' + undefined = '1undefined'
'1' + null = '1null'
依旧会把非字符串强制转换为字符串然后拼接
字符串 + 数组
'1' + [] = '1'
'1' + [123] = '1123'
'1' + [, , ,] = '1,,'
'1' + [123, 456, 789, 'aaa'] = '1123,456,789,aaa'
数组转换为字符串会是把里面的值转换成字符串,[]会被转换成"",但是如果是保留位置的数组,则会去除最后一个逗号转换成字符串
字符串 + 函数
'1' + function() {console.log(123)} = '1function() {console.log(123)}'
函数会被转换成字符串,然后拼接
字符串 + 对象
'1' + {} = '1[object Object]'
'1' + {a: 1} = '1[object Object]'
会先把对象转换成字符串 ‘[object Object]’ 再拼接
数值 + 非数值
1 + NaN = NaN
1 + undefined = NaN
1 + Infinity = Infinity
1 + -Infinity = -Infinity
1 + null = 1
1 + true = 2
1 + false = 1
数值 + 数组
[] + 1 = '1'
[123] + 1 = '1231'
[123, 456, 789] + 1 = '123,456,7891'
[, , ,] + 1 = ',,1'
[, 1, 1,] + 1 = ',1,11'
{} + 1 = 1
数组 + 函数
[] + function() {console.log(123)} = 'function() {console.log(123)}'
[1, 2] + function() {console.log(123)} = '1,2function() {console.log(123)}'
数组 + 对象
[] + {} = '[object Object]'
[1,2] + {} = '1,2[object Object]'
[1,2] + {a: 1} = '1,2[object Object]'
数组 + 数组
[1,2] + [3, 4] = '1,23,4'
[] + [1,2] = '1,2'
对象 + 对象
{} + {} = '[object Object][object Object]'
({}) + ({}) = '[object Object][object Object]'
({}) + ({a: 1}) = '[object Object][object Object]'
对象 + 函数
{} + function() {console.log(123)} = '[object Object]function() {console.log(123)}'
对象 + 数组 (特例)
{} + [] = 0
{} + ['1'] = 1
{} + [1] = 1
{} + [1, 2] = NaN
{} + ['1', '2'] = NaN
({}) + [] = '[object Object]'
函数 + 函数
function() {console.log(123)} + function() {console.log(123)} 报错
(function() {console.log(123)}) + (function() {console.log(123)}) = 'function() {console.log(123)}function() {console.log(123)}'
1、数值与数值相加直接求和
2、任意数据类型 + NaN = NaN
3、Infinity + - Infinity = NaN
4、只有-0 + -0 = -0,其余得+0
5、字符串 + 任意数据,先将其他转换为字符串,再拼接,小数最后一位为0时转字符串时该0会被去除
6、[, , ,]数组最后为逗号 , 时,会去除最后一个逗号 , 再将数组内部元素转成字符串
7、空数组[]转成字符串为 “”
8、正常情况含有元素的数组会把里面的元素拼接成字符串包含逗号 , ,
9、正常情况{} 转成字符串无论里面是否为空均转为[object Object],({})也是转成[object Object]
10、特例:{} + []或者{} + [1]或者{} + [‘1’]会直接将 {}转成0 []转成0 [‘1’]转成1
12、特例:{} + [1, 2] = NaN
13、函数也会被整体转成字符串,但 函数 + 函数会报错, (函数) + (函数) 才会进行两个转换之后的字符串拼接
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。