Created
November 17, 2010 17:30
-
-
Save mnelson/703695 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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(); | |
} |
This file contains hidden or 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
SomeClass = new Class({ | |
initialize : $empty, | |
someFunction : function(){ return // some code; }, | |
Memoize : ['someFunction'] | |
}); |
This file contains hidden or 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
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