Skip to content

Instantly share code, notes, and snippets.

@mnelson
Created November 17, 2010 17:30
Show Gist options
  • Save mnelson/703695 to your computer and use it in GitHub Desktop.
Save mnelson/703695 to your computer and use it in GitHub Desktop.
Class.Mutators.Memoize = function(method_names){
Array.from(method_names).each(function(method){
var old_method = this.prototype[method];
this.prototype[method] = function(){
if(this.__memoized[method] !== undefined) return this.__memoized[method];
return this.__memoized[method] = old_method.apply(this, arguments);
};
}, this);
this.prototype.unmemoize = function(key){
var val = this.__memoized[key];
this.__memoized[key] = undefined;
return val;
}
this.prototype.unmemoizeAll = function(){ this.__memoized = {}; }
this.prototype.unmemoizeAll();
}
SomeClass = new Class({
initialize : $empty,
someFunction : function(){ return // some code; },
Memoize : ['someFunction']
});
SomeClass = new Class({
initialize : $empty,
someFunction : function(){
if(this.someValue !== undefined) return this.someValue;
this.someValue = // some code to set variable
return this.someValue;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment