Created
December 9, 2010 13:13
-
-
Save acrosa/734699 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Given an Object and a function | |
var obj = { name: "Alejandro" }; | |
var func = function(message) { return message +" "+ this.name; }; | |
var f = _.bind(func, obj); // Bind it! | |
f("Hello there") | |
=> "Hello there Alejandro" | |
// If you pass a third argument you can pre fill the arguments of the function | |
// This is called currying, in this case we default to "Hello there" | |
var f1 = _.bind(func, obj, "Hello there"); | |
f1() | |
=> "Hello there Alejandro" | |
// Let's bind the same function, but with different arguments | |
var f2 = _.bind(func, obj, "Hola ") | |
f2() | |
=> "Hola Alejandro" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment