Search code examples
ecmascript-6ecmascript-5

What is the meaning of this note in the spec?


What is the meaning of this? This is regarding the Array.prototype.concat method, but it's stated throughout the spec, again and again, for a series of methods.

The concat function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether the concat function can be applied successfully to a host object is implementation-dependent.

Can you explain this in simple wording, perhaps providing an example as well?


Solution

  • It means that you can apply the method to an array-like object even if it's not an Array. These include NodeLists and the arguments object.

    I sometimes use this feature to turn the arguments object into an array: [].slice.call(arguments) returns the argument list as an Array object, giving me native access to all the array methods.

    You can read about this on MDN.