Skip to content

Instantly share code, notes, and snippets.

@davidaurelio
Last active December 14, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidaurelio/5099695 to your computer and use it in GitHub Desktop.
Save davidaurelio/5099695 to your computer and use it in GitHub Desktop.
/**
* From AS2:
* class A {
* var foo = 5;
* }
*
* class A extends B {
* var foo = 6;
* }
*/
function A() {}
Object.defineProperties(A.prototype, {
foo: lazyProperty('foo', function() { return 5; });
});
function B() {}
B.prototype = Object.create(A.prototype, {
foo: lazyProperty('foo', function() { return 6; });
})
function lazyProperty(propertyName, factory) {
return {
get: function() {
return this[propertyName] = factory();
},
set: function(value) {
Object.defineProperty(this, propertyName, {value: value, writable: true});
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment