Ecma262: Array.prototype.concat `this` value oddity

Created on 26 Dec 2019  路  6Comments  路  Source: tc39/ecma262

Description:
Array.prototype.concat.call(1, 2, 3) returns the value [ [Number: 1], 2, 3 ], where the first argument (the this), if primitive, has been converted to an object.

This seems like an undesirable asymmetry with the remaining arguments.

The algorithm appears to only need the object for ArraySpeciesCreate(), not for processing the list of elements.

question

All 6 comments

The receiver isn鈥檛 the same as an argument (although that鈥檚 one way to think of it); what symmetry would you envision?

All of the array methods, i believe, operate on the receiver in this way.

I'm imagining the desired result is an array that is composed entirely of the values, or elements thereof, that I sent into the call. However, in the case i cite, the resulting array contains a manufactured value that is nowhere to be found in my call.

If you did [].concat(1, 2, 3) you鈥檇 get that (or Array.of(1, 2, 3)), but borrowing a prototype method and using .call on it isn鈥檛 something I鈥檇 consider common usage.

using .call on it isn鈥檛 something I鈥檇 consider common usage

Nor I. I just thought is was a weird leakage of the algorithm's internal temp value.

You鈥檒l find this in other generic Array methods even if they don鈥檛 invoke ArraySpeciesCreate.

image

I鈥檓 not sure I鈥檇 consider it a leak, since concat鈥檚 apparent flexibility is what鈥檚 unusual here moreso than generic arraylike methods casting their receivers to generic arraylikes (which === 'any object').

None of this is an implementation leak, rather the implementation is carefully preserving the legacy behavior from when the methods were first added to the standard. Don't break the web!

Was this page helpful?
0 / 5 - 0 ratings