Вызывая this
внутри установщика, вы ссылаетесь на объект state
, для которого не определено свойство other
(Проверьте второй файл console.log, который ссылается на this._bar
).
Вы можете сохранить this
в переменной (self) следующим образом:
class Foo {
constructor() {
const self = this;
this.other = 'i am an other variable'
this.state = {
_bar: 'default',
set bar(flag) {
console.log(self.other);
console.log(this._bar);
this._bar = flag
},
get bar() {
return this._bar
}
}
}
}
const foo = new Foo()
foo.state.bar = 'yolo'