Ответ 1
Вам нужно использовать ключевое слово this
вместо self
.
runMyTest() {
this.myTest();
}
Примечание стороны
Если вы вложенность стандартных функций обозначения, то this
не лексический переплетен (будет определенно). Чтобы обойти это, используйте функции со стрелками (предпочтительно), .bind
или локально определите this
вне функции.
class Test {
constructor() {
this.number = 3;
}
test() {
function getFirstThis() {
return this;
}
const getSecondThis = () => {
return this;
};
const getThirdThis = getFirstThis.bind(this);
const $this = this;
function getFourthThis() {
return $this;
}
// undefined
console.log(getFirstThis());
// All return "this" context, containing the number property
console.log(this);
console.log(getSecondThis());
console.log(getThirdThis());
console.log(getFourthThis());
}
}
new Test().test();