Skip to content

Instantly share code, notes, and snippets.

@biogeo
Created November 29, 2020 22:49
Show Gist options
  • Save biogeo/00ae6e82789f4956a678116d01be3c92 to your computer and use it in GitHub Desktop.
Save biogeo/00ae6e82789f4956a678116d01be3c92 to your computer and use it in GitHub Desktop.
Prototype-changing objects in Javascript
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