Created
November 29, 2020 22:49
-
-
Save biogeo/00ae6e82789f4956a678116d01be3c92 to your computer and use it in GitHub Desktop.
Prototype-changing objects in Javascript
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 Mutating { | |
constructor(x) { | |
this._thing = x; | |
} | |
get thing() { return this._thing; } | |
set thing(x) { | |
this._thing = x; | |
this.change(); | |
} | |
} | |
class MutatingA extends Mutating { | |
change() { Object.setPrototypeOf(this, MutatingB.prototype); } | |
announce() { return 'A...'; } | |
get stuff() { return 'A!'; } | |
} | |
class MutatingB extends Mutating { | |
change() { Object.setPrototypeOf(this, MutatingA.prototype); } | |
announce() { return 'B?'; } | |
get stuff() { return 'B!'; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment