Skip to content

Instantly share code, notes, and snippets.

@acrosa
Created December 9, 2010 13:13
Show Gist options
  • Save acrosa/734699 to your computer and use it in GitHub Desktop.
Save acrosa/734699 to your computer and use it in GitHub Desktop.
// 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