Skip to content

Instantly share code, notes, and snippets.

@savelichalex
Created October 30, 2017 09:37
Show Gist options
  • Save savelichalex/2f48ddbad4b36c927a52f87a04b2ea0d to your computer and use it in GitHub Desktop.
Save savelichalex/2f48ddbad4b36c927a52f87a04b2ea0d to your computer and use it in GitHub Desktop.
class Immutable {
constructor(of) {
Object.keys(of).forEach(key => Object.defineProperty(this, key, { enumerable: true, get: () => this.of[key] }));
Object.defineProperty(this, "of", { enumerable: false, get: () => of });
}
set(key, value) {
return new this.constructor({ ...this.of, [key]: value });
}
}
class Foo extends Immutable {
constructor(obj) {
super(obj);
this.fooClass = true;
}
setFoo(val) {
return super.set("foo", val);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment