Created
January 5, 2012 21:47
-
-
Save DavidBruant/1567494 to your computer and use it in GitHub Desktop.
Object.prototype.boundTo
This file contains 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
"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