Asked 7 years ago
18 Jan 2017
Views 1168
web-api

web-api posted

use of apply() method in JavaScript ?

what use of apply() method in JavaScript ?
myArray.apply('a') means it append to myArray or what ?
or apply() method means apply logic for all array value ?
Rasi

Rasi
answered Nov 30 '-1 00:00


let me explain apply method


function circle(r,d) {
return r*d;
 }
var circleMeta = [12,12];//r,d
circle.apply(null, circleMeta );


if we want to pass all array value to argument of function than apply is good to use.
above code return two array value multiplication
it make array to flat value and pass to function argument as order as it in array in JavaScript
Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00

apply() method is like call() method .

apply method call function with given array 's value as argument
funct.apply(null,argsArray)
argsArray ' s all value passed to funct as argument .

spread syntax is work like same . it spread the array 's value

funct.apply(null,argsArray)==funct(...argsArray);


so pass spread syntax as argument is equal to use apply method .
Post Answer