调用函数变量而不是JavaScript / TypeScript中的函数

时间:2019-01-23 00:26:21

标签: javascript closures this

class component()
{
    Service _service = new Service();
    let serviceMethod = isUpdate ? this._service.update : 
    this._service.create;

    serviceMethod(param);
}

class Service
{
    update()
    {
        // Other stuff
        // Cannot access this here
        this.privatelocalfunc();
    }

    create()
    {
       // Other stuff
       // Cannot access this here
       this.privatelocalfunc();
    }
}

我必须有条件地在上述代码中描述的方法之间进行切换。但是,当我这样做时,我无法访问service方法的'this'变量。什么是实现这一目标的最佳方法?

0 个答案:

没有答案