Created
December 3, 2012 08:54
-
-
Save davidaurelio/4193722 to your computer and use it in GitHub Desktop.
Lazy properties for ES5
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
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