Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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