Not Able To Update A Class Property In Es6
How does this translate to ES6? I've tried this: But it doesn't work because the class properties are actually attached directly to the instanciated object instead of being attac
Solution 1:
It seems an antipattern to me to change the prototype. But if you really want this, you should just continue to use the same syntax as before for defining the property:
classMock { }
Mock.prototype.foo = "bar";
const obj = newMock();
console.log(obj.foo);
Mock.prototype.foo = "Something else";
console.log(obj.foo);
Solution 2:
- Because in ES6, the .prototype property of classes is not writable and not configurable
- If you wanna change then use Object.defineProperty
classMock {
foo = 'bar'
}
const obj = newMock()
console.log(obj.foo)
Object.defineProperty(obj, 'foo', {
value: 'Something else',
writable: true,
enumerable: false,
configurable: false
})
console.log(obj.foo)
Post a Comment for "Not Able To Update A Class Property In Es6"