Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
Created January 5, 2012 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBruant/1567494 to your computer and use it in GitHub Desktop.
Save DavidBruant/1567494 to your computer and use it in GitHub Desktop.
Object.prototype.boundTo
"use strict";
(function(){
var cache = new WeakMap();
Object.prototype.boundTo = function(fct){
var obj = this;
var objMap = cache.get(fct);
if(objMap === undefined){
objMap = new WeakMap();
cache.set(fct, objMap);
}
var bound = objMap.get(obj);
if(bound === undefined){
bound = fct.bind(obj);
objMap.set(obj, bound);
}
return bound;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment