Skip to content

Instantly share code, notes, and snippets.

@aoberoi
Created May 21, 2012 23:53
Show Gist options
  • Save aoberoi/2765473 to your computer and use it in GitHub Desktop.
Save aoberoi/2765473 to your computer and use it in GitHub Desktop.
Bind generic values to make specialized versions of a function
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