Skip to content

Instantly share code, notes, and snippets.

@amaxwell01
Created August 6, 2013 16:54
Show Gist options
  • Select an option

  • Save amaxwell01/6166340 to your computer and use it in GitHub Desktop.

Select an option

Save amaxwell01/6166340 to your computer and use it in GitHub Desktop.
How to use the bind method to perform currying
var multiply = function(x) {
return x * 2;
};
var myMath = {
_multiplyBy: 5,
multiply: function(x) {
return x * this._multiplyBy;
},
multiplyBy: function() {
return this.multiply(5);
}
};
var mathBind = myMath.multiplyBy.bind(myMath);
var mathNoBind = myMath.multiplyBy;
console.log( 'Module example: ', myMath.multiplyBy() ); // Expect 25
console.log( 'Bind example: ', mathBind() ); // Expect 25
console.log( 'NoBind example: ', mathNoBind() ); // Expect 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment