【发布时间】:2021-10-17 12:18:15
【问题描述】:
所以我有类 MyClass,它有方法 myFunction,这个方法接受对象数组,在这个对象中有方法 func,我想用 func 方法访问这个对象内部的这个方法索引或名称属性。怎么办?
const object = [{
index: 1,
name:"John",
func: async(store) => {
store[this.index] = "this text";
}
}];
class MyClass {
constructor() {
this.store = {};
}
async myFunction(object) {
for (let i in object) {
await object[i].func(this.store);
}
}
}
const myClass = new MyClass();
(async() => {
try {
await myClass.myFunction(object);
const store = myClass.store;
console.log(store);
} catch (err) {
console.log(err);
}
})();
【问题讨论】:
标签: javascript object asynchronous arrow-functions