Skip to content

Instantly share code, notes, and snippets.

@kaaes
Created May 19, 2011 14:59
Show Gist options
  • Save kaaes/980945 to your computer and use it in GitHub Desktop.
Save kaaes/980945 to your computer and use it in GitHub Desktop.
bind() example
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