Created
May 21, 2012 23:53
-
-
Save aoberoi/2765473 to your computer and use it in GitHub Desktop.
Bind generic values to make specialized versions of a function
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 myCoolFunc, getContext; | |
getContext = function () { | |
return "specific context"; | |
}; | |
// generic function | |
myCoolFunc = function (param1, context) { | |
console.log(param1 + ' ' + context) | |
}; | |
// use a generic form of the function | |
myCoolFunc('which context?', 'generic'); | |
// bind the function to some specific context | |
myCoolFunc = (function() { | |
var specificContext, genericCoolFunc; | |
specificContext = getContext(); | |
genericCoolFunc = myCoolFunc; | |
return function (param1) { | |
return genericCoolFunc(param1, specificContext); | |
}; | |
}()); | |
// use a specific form of the function | |
myCoolFunc('which context?'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment