Created
August 6, 2013 16:54
-
-
Save amaxwell01/6166340 to your computer and use it in GitHub Desktop.
How to use the bind method to perform currying
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
| 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