Javascript ';这';在课堂上没有定义

Javascript ';这';在课堂上没有定义,javascript,class,asynchronous,Javascript,Class,Asynchronous,看看下面的代码 我正在用构造函数创建一个类,在这个类中我正在创建一些变量。此外,我正在创建一些异步函数,其中3个非常相似,最后一个可以调用其他函数 class Project { data = {}; constructor(project, api) { for (const key in project) { this.data[key] = project[key]; } this.logger = n

看看下面的代码

我正在用构造函数创建一个类,在这个类中我正在创建一些变量。此外,我正在创建一些异步函数,其中3个非常相似,最后一个可以调用其他函数

class Project {
    data = {};
    constructor(project, api) {
        for (const key in project) {
            this.data[key] = project[key];
        }
        this.logger = new Logger(
            document.querySelector("#output"),
            `Project= ${this.data.projectId}: `
        )
        this.api = api;
    }
    async getTasks() {
        return await this.api.getTasksAsync(this.projectId);
    }
    async getRequirements() {
        return await this.api.getRequirementsAsync(this.projectId);
    }
    async getReleases() {
        return await this.api.getReleasesAsync(this.projectId);
    }
    async getToDraw(type) {
        let func = this[`get${type}`];
        console.log(func);
        let result = [];
        for (let item of await func()){
            result.push(toTaskItem(item));
        }
        return result;
    }
}
在类“Project”(如上)的已启动对象上执行函数“getToDraw”时出现的错误:

我按如下方式调用该函数:

async drawDiagram(sender){
    let projectID = this.getSelectedValue("projectSelector");
    if (projectID !== "-1") {
        let project = this.projects.filter((project) => project.data.ProjectId === parseInt(projectID))[0];
        if (document.querySelector("#typeSelector").selectedOptions[0].innerText === "Full"){
            // TODO: 
        } else {
            let list = await project.getToDraw(document.querySelector("#typeSelector").selectedOptions[0].innerText);
            console.log(list);
        }
    } else {
        for (let project of this.projects) {
            // TODO:
        }
    }
}
如果没有人知道我的解决方案,我已经知道如何用另一种方式来做,但我想这样做


格雷西亚斯,各位。

这是因为当您将
getRequirements
方法存储在
func
变量中时,您将其与其上下文分离

必须将
上下文重新附加到
函数

第一种方法:只附加一次上下文 您只需像这样调用
func
函数:

func.call(this); // call func with this as context
func(); // call func with undefined context
func.call({}); // call func with empty object as context
let func = this[`get${type}`].bind(this);
class Project {
  data = {};
  getTasks = () => { ... };
  getRequirements = () => { ... };
  getReleases = () => { ... };
}
这样,您就可以强制调用
func
,并将
This
作为上下文

第二种方法:为将来的所有调用附加上下文 在调用函数之前,您必须绑定一个新的上下文:

func(); // call func with undefined context
func = func.bind(this);
func(); // call func with this as context
func(); // recall func with this as context
这样,您可以将
这个
链接为
函数的新上下文

请看这一行

let func = this[`get${type}`];
它将函数从类的
this
引用中解除绑定(或“断开”),以便在调用
func()
时,
this
变为
未定义的

有几种方法可以解决这个问题。您可以使用重新绑定
,如下所示:

func.call(this); // call func with this as context
func(); // call func with undefined context
func.call({}); // call func with empty object as context
let func = this[`get${type}`].bind(this);
class Project {
  data = {};
  getTasks = () => { ... };
  getRequirements = () => { ... };
  getReleases = () => { ... };
}
或者可以在构造函数中显式重新绑定方法:

class Project {
  constructor(project, api) {
    ...
    this.getTasks = this.getTasks.bind(this);
    this.getRequirements = this.getRequirements.bind(this);
    this.getReleases = this.getReleases.bind(this);
  }
}
也可以将方法定义为lambda表达式属性,如下所示:

func.call(this); // call func with this as context
func(); // call func with undefined context
func.call({}); // call func with empty object as context
let func = this[`get${type}`].bind(this);
class Project {
  data = {};
  getTasks = () => { ... };
  getRequirements = () => { ... };
  getReleases = () => { ... };
}

当然,这些并不是唯一的解决方案,它们可能有一些不同的副作用。例如,lambda表达式属性是可枚举的,因此它们将出现在
Object.keys(new Project())

如何调用该方法?如何实例化对象以及如何调用该方法?或者您可以只调用该方法,而不将其放入变量
this['get${type}]()