Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abrjagad/8971915 to your computer and use it in GitHub Desktop.
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
//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