call() and apply() are two predefined javascript methods. We use both of them to invoke a function & these method must have its own object as a first parameter.
apply allow us to invoke the function with passing the arguments in the form of array.
Example:
function myFunction1(a, b) {
return a + b;
}
var myobj1 = new Object();
var result1 = myFunction1.call(myobj1, 5, 6);
console.log('Result with call function: ' + result1);
call always requires that all parameters will be listed explicitly not implicitly and one by one.
Example:
function myFunction2(a, b) {
return a + b;
}
myArray = [5, 6];
var myobj2 = new Object();
var result2 = myFunction2.apply(myobj2, myArray);
console.log('Result with Apply function: ' + result2);
0 Comment(s)