Created
May 19, 2011 14:59
-
-
Save kaaes/980945 to your computer and use it in GitHub Desktop.
bind() example
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
function sum () { | |
var s = 0; | |
for (var i = 0, len = arguments. length; i < len; i++) { | |
s += arguments[i]; | |
} | |
return s; | |
} | |
// execute function and | |
// return 6, context is window | |
var simpleSum = sum(1, 2, 3) | |
// preset first argument, 'this' will be null | |
// don't execute function yet! | |
var addToOne = sum.bind(null, 1); | |
// return 8 - 1 will be the first argument | |
addToOne(3, 4); | |
// preset two first arguments, 'this' will be null | |
var addToThree = sum.bind(null, 1, 2); | |
// return 10 | |
addToThree(3, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment