Skip to content

Instantly share code, notes, and snippets.

@davidaurelio
Created December 3, 2012 08:54
Show Gist options
  • Save davidaurelio/4193722 to your computer and use it in GitHub Desktop.
Save davidaurelio/4193722 to your computer and use it in GitHub Desktop.
Lazy properties for ES5
function lazyProperty(object, name, create) {
Object.defineProperty(object, name, {
configurable: true, // or false if defined on prototype objects
enumerable: false,
get: function() {
return this[name] = create(this);
},
set: function(value) {
Object.defineProperty(this, name, {
configurable: true,
enumerable: false,
value: value,
writable: true
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment