Last active
December 14, 2015 14:19
-
-
Save davidaurelio/5099695 to your computer and use it in GitHub Desktop.
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
/** | |
* 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; }); | |
}) |
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(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