Search This Blog

Showing posts with label different between apply and call. Show all posts
Showing posts with label different between apply and call. Show all posts

Friday, September 27, 2013

Javscript: the different between apply and call.

apply lets you invoke the function with arguments as an array; callrequires the parameters be listed explicitly.


Pseudo syntax:
theFunction.apply(valueForThis, arrayOfArgs)
theFunction.call(valueForThis, arg1, arg2, ...)
Sample code:
function theFunction(name, profession) {
    alert("My name is " + name + " and I am a " + profession + ".");
}
theFunction("John", "fireman");
theFunction.apply(undefined, ["Susan", "school teacher"]);
theFunction.call(undefined, "Claude", "mathematician");
Source: http://stackoverflow.com/questions/1986896/what-is-the-difference-between-call-and-apply