I understand how the concat
method works, but I had one question about a line in the MDN docs.
It says:
The
concat
methods does not alterthis
or any of the arrays provided as arguments but instead returns a shallow copy that contains copies of the same elements combined from the original arrays.
My questions is, how is it possible for it to potentially alter the this
? Is it saying that the argument arrays could potentially lose their this
but instead it preserves it since it's simply creating a new array?
In the call a.concat(b, c)
, the function being called is a.concat
, the this value is a
, and the arguments are b
and c
. What MDN is saying there is that the array a
is not changed by that call – unlike, for example, 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)` changed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With