阵列串联与+和concat不同

xam*_*mir -2 html javascript arrays html5 concatenation

这个小提琴中,当使用+运算符时,,跳过分隔符,而concat方法不会跳过,.

在javascript中,通常与+运算符和concat方法的串联是相同的.它也不适用于数组吗?

and*_*lrc 9

考虑一下:

[1, 2, 3] + [4, 5, 6] // "1,2,34,5,6"
[1, 2, 3].concat([4, 5, 6]) // [1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)

在数组上使用plus concatenation运算符将使数组经历以下步骤:

[1, 2, 3] + [4, 5, 6]
[1, 2, 3].toString() + [4, 5, 6].toString()
"1,2,3" + "4,5,6"
"1,2,34,5,6"
Run Code Online (Sandbox Code Playgroud)

  • @student:不,这是因为没有为数组定义`+`,所以两个操作数都转换为字符串. (2认同)