Javascript 为什么将元素推入concat()返回的新数组会返回数组的大小而不是数组本身? 变量a=['a','b']; 变量b=['c','d']; var c=a.concat(b.push('e'); document.getElementById(“demo”).innerHTML=c;

Javascript 为什么将元素推入concat()返回的新数组会返回数组的大小而不是数组本身? 变量a=['a','b']; 变量b=['c','d']; var c=a.concat(b.push('e'); document.getElementById(“demo”).innerHTML=c;,javascript,arrays,concat,chain,Javascript,Arrays,Concat,Chain,这将导致数字“5”,而不是['a'、'b'、'c'、'd'、'e']根据定义,该方法返回调用该方法的对象的新length属性 方法所基于的对象的新长度属性 打电话来 这里, 它依次返回新形成的数组的长度。 因此,语句的最终返回值是数组的长度,它存储在c变量中 要通过concat()操作捕获返回的数组,您可以修改代码,将链式方法分解为多个语句,如下所示: a.concat(b) //returns an `array`. But wait, the statement still has a me

这将导致数字“5”,而不是['a'、'b'、'c'、'd'、'e']

根据定义,该方法返回调用该方法的对象的新
length
属性

方法所基于的对象的新长度属性 打电话来

这里,

它依次返回新形成的数组的
长度
。 因此,语句的最终返回值是数组的
长度
,它存储在
c
变量中

要通过
concat()
操作捕获返回的
数组
,您可以修改代码,将链式方法分解为多个语句,如下所示:

a.concat(b) //returns an `array`. But wait, the statement still has a method chained,
            //and to be evaluated.
(returned array).push('e'); // the chained push() is invoked on the returned array.

文档的第一句话:“不确定您所说的为什么……因为这是文档所规定的。在提出问题之前阅读该方法的文档提示:Google search”javascript Array push MDN“如果要返回最后一个值,请使用结果长度作为查找的一部分。当然,这可能会让其他人在阅读代码时感到困惑:
var val=arr[arr.push(“val”)-1]
@Ja͢ck-可能是OP被与
concat()
push()
方法的链式应用程序混淆了。
除非出现“;”否则语句不会结束遇到
-;)
a.concat(b) //returns an `array`. But wait, the statement still has a method chained,
            //and to be evaluated.
(returned array).push('e'); // the chained push() is invoked on the returned array.
var c = a.concat(b);
c.push('e');
console.log(c) // prints the array content.