Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply function in JavaScript

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?

like image 549
eljenso Avatar asked Dec 29 '11 15:12

eljenso


People also ask

What is Apply function in JavaScript?

The apply() method calls the specified function with a given this value, and arguments provided as an array (or an array-like object).

What is the difference between call () and apply ()?

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.

What is function apply?

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.

Why do we use apply method?

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.


1 Answers

[].concat is Array.prototype.concat.

Array.concat is a Firefox-only static method that concatenates one or more arrays and ignores its this argument.

like image 196
SLaks Avatar answered Sep 28 '22 01:09

SLaks