Created
February 13, 2014 08:54
-
-
Save abrjagad/8971915 to your computer and use it in GitHub Desktop.
Explaining Call and Apply; basically call and apply Invoke a Function with the given set of arguments
This file contains hidden or 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
//Invocation with the apply() and call() methods | |
// create a plain function | |
function juggle() { | |
var result = 0; | |
for (var n = 0; n < arguments.length; n++) { | |
result += arguments[n]; | |
} | |
//this.result = result; | |
console.log(result) | |
} | |
//Invloke with plain arguments | |
juggle(1, 2, 3, 4)//10 | |
//create two objects | |
var ninja1 = {}; | |
var ninja2 = {}; | |
// Execute the function juggle with new arguments | |
juggle.apply(ninja1, [1, 2, 3, 4]);//10 | |
juggle.apply(null, [1, 2, 3, 4]);//10 | |
juggle.call(ninja2, 5, 6, 7, 8);//26 | |
juggle.call(null, 5, 6, 7, 8);//26 | |
//console.log(ninja1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment