【发布时间】:2021-11-30 01:03:17
【问题描述】:
我目前正在从事一个项目,我想在其中尊重一组函数(函数引用)并执行该函数。
这只有在我不在函数中调用另一个类方法时才有效。 否则我会得到“Uncaught TypeError”,我不知道如何解决这个错误。
这是我的代码示例“工作”的方式与我的原始项目相同: 调用function2后引擎找不到this.log...
你有想法吗?非常感谢您。
KR,罗伯特
class ArrayWithFunctions {
constructor() {
this.functionTable = [
this.function1,
this.function2,
];
}
execute(index) {
return (this.functionTable[index])();
}
log(chars) {
console.log(chars);
}
function1() {
console.log('I am Function 1.');
}
function2() {
this.log('I am Function 2.');
}
}
let example = new ArrayWithFunctions();
example.execute(0);
example.execute(1);
【问题讨论】:
标签: javascript arrays function