Skip to content

Instantly share code, notes, and snippets.

@eshacker
Created February 1, 2016 09:00
Show Gist options
  • Save eshacker/468dd60bf579120a1ab2 to your computer and use it in GitHub Desktop.
Save eshacker/468dd60bf579120a1ab2 to your computer and use it in GitHub Desktop.
Apply and Call in JavaScript
/* when there is no difference in apply and call */
// 1.
var func = function(arr, barr, carr){
this.arr = arr;
console.log(typeof(arr), arr.length, arr, barr, carr);
};
func.call(null, 1, 2, 3); // number undefined 1 2 3
func.apply(null, [1, 2, 3]); // number undefined 1 2 3
// 2.
console.log(Math.min.call(null, 1, 2, 3));
console.log(Math.min.apply(null, [1, 2, 3]));
/* when call and apply had differences */
// 1.
var diff1 = "call() accepts an argument list, while apply() accepts a single array of arguments.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment