Last active
November 30, 2017 10:19
-
-
Save malys/9db93b0bc057694b4201 to your computer and use it in GitHub Desktop.
[Apply vs Call] #javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//From http://stackoverflow.com/questions/1986896/what-is-the-difference-between-call-and-apply | |
/*The main difference is that apply lets you invoke the function with arguments as an array; | |
call requires the parameters be listed explicitly. | |
See here and here. | |
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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment