I found some code that uses a decorator as an instance variable in a class...
I tried to replicate this in plain Javascript, but it doesn't work.
This is the Typescript:
export function ObservableProperty() {
return (obj: Observable, key: string) => {
let storedValue = obj[key];
Object.defineProperty(obj, key, {
get: function () {
return storedValue;
},
set: function (value) {
if (storedValue === value) {
return;
}
storedValue = value;
this.notify({
eventName: Observable.propertyChangeEvent,
propertyName: key,
object: this,
value
});
},
enumerable: true,
configurable: true
});
};
}
Then, in the class:
export class MyClass extends Observable {
@ObservableProperty() public theBoolValue: boolean;
…
I tried all sorts of ways to instantiate my JS-variable - always get errors…
Like:
@ObservableProperty
this.theBoolValue = false;
Is this even possible?