Я пытаюсь создать класс, который выглядит следующим образом:
class Foo {
_bar?: () => void; // this is a property which will invoke a function, if it is defined
set bar(barFunctionDef: () => void) { // this stores a reference to the function which we'll want to invoke
this._bar = barFunctionDef;
}
get bar() { // this should either invoke the function, or return a reference to the function so that it can be invoked
return this._bar;
}
doSomething() { // this is what will be called in an external class
if(condition) {
this.bar; // this is where I would like to invoke the function (bar)
}
}
}
По сути, я хочу иметь класс, который будет хранить ссылки на функции, которые будут установлены по желанию. Будет много свойств класса "bar", которые будут иметь тип () => void
.
. Есть ли "правильный" способ вызова this.bar
внутри doSomething
, чтобы вызвать функцию, которая хранится на это имущество?