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
Object.memoize = function(obj, defs) { | |
function add(key, getter) { | |
obj.__defineGetter__(key, function() { | |
delete this[key]; | |
return this[key] = getter.call(this); | |
}); | |
} | |
for (let key in defs) { | |
if (defs.hasOwnProperty(key)) { | |
add(key, defs[key]); | |
} | |
} | |
}; | |
// Example (function "val1" is executed only once) | |
// var obj = { foo: 0, bar: 1 }; | |
// Object.memoize(obj, | |
// {prop1: function val1() { /* very heavy acrion */ return val; }, | |
// prop2: function val2() { ... }}); | |
// console.log(obj.prop1); | |
// console.log(obj.prop1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment