qar*_*dso 4 javascript arrays this
我理解该concat方法是如何工作的,但我有一个关于MDN文档中的一行的问题.
它说:
这些
concat方法不会改变this或作为参数提供的任何数组,而是返回一个浅拷贝,其中包含从原始数组组合的相同元素的副本.
我的问题是,它怎么可能改变this?是不是说参数数组可能会丢失它们this而是保留它,因为它只是创建一个新数组?
在调用中a.concat(b, c),被调用的函数是a.concat,这个值是a,参数是b和c.MDN说的是,该a调用不会改变数组- 例如,不像a.push(x).
const arr = [1, 2, 3];
arr.concat([4, 5]);
console.log(arr.join(', '));
// the “this” array in the call `arr.concat([4, 5])` didn’t change
arr.push(6);
console.log(arr.join(', '));
// the “this” array in the call `arr.push(6)` changedRun Code Online (Sandbox Code Playgroud)