var a = 'QnA.habr.com';
Object.defineProperty(this, 'a', {
set: function(v) {console.log('Изменяют!'); this.value = v;},
value: 'QnA.habr.com'
});
то теперь любое изменение значения переменной вызовет панику в консоли:a = 'StackOver...';
// Изменяют!
// tracked variable
let num = 1;
// add an observer, pass the name and function that returns the variable
trackChanges.addObserver('numObserv', () => num);
// add a handler, pass the name and function that will be called when changing "num"
trackChanges.addHandler('numObserv', numHandler);
/**
create a function that will be called when the variable changes, the function argument will be its changed value
**/
function numHandler(modifiedResult) {
console.log(`Num changed to: ${ modifiedResult }`);
}
class foo {
private _bar: boolean = false;
get bar(): boolean {
return this._bar;
}
set bar(value: boolean) {
this._bar = value;
}
}