Created
February 1, 2016 09:00
-
-
Save eshacker/468dd60bf579120a1ab2 to your computer and use it in GitHub Desktop.
Apply and Call in 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
/* 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