I'm learning JavaScript and I'm currently trying to figure out why (in Spidermonkey)
[].concat.apply([1], [[2]])
returns the expected [1, 2]
, but
Array.concat.apply([1], [[2]])
returns [2]
instead of [1, 2]
.
Could someone provide a good explanation?
The apply() method calls the specified function with a given this value, and arguments provided as an array (or an array-like object).
The Difference Between call() and apply() The difference is: The call() method takes arguments separately. The apply() method takes arguments as an array. The apply() method is very handy if you want to use an array instead of an argument list.
The apply() method is used to write methods, which can be used on different objects. It is different from the function call() because it takes arguments as an array. Return Value: It returns the method values of a given function.
The apply() method is an important method of the function prototype and is used to call other functions with a provided this keyword value and arguments provided in the form of array or an array like object. Array-like objects may refer to NodeList or the arguments object inside a function.
[].concat
is Array.prototype.concat
.
Array.concat
is a Firefox-only static method that concatenates one or more arrays and ignores its this
argument.
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