简体   繁体   English

第三方库绑定了回调,但是我需要访问类属性

[英]Third party library is binding callbacks, but I need to access class properties

I've seen several questions about using "this" in classes and functions, but I don't think I've seen what I'm looking for in particular. 我已经看到了一些有关在类和函数中使用“ this”的问题,但是我认为我没有特别在寻找什么。

My situation is: 我的情况是:

I'm calling a function from a third-party library in a class method. 我在类方法中从第三方库调用函数。 However, the third-party library function is calling callback.bind(this), and I need to have access to the context it's binding. 但是,第三方库函数正在调用callback.bind(this),并且我需要访问其绑定的上下文。

But I also want to be able to access class properties. 但我也希望能够访问类属性。 Is this possible? 这可能吗? If not, what are some potential workaround? 如果没有,有什么可能的解决方法? Code outline looks something like: 代码大纲如下所示:

class MyClass {
  myProperty = 'something';

  myMethod() {
    console.log(this.myProperty);
  }

  otherMethod() {
     thirdPartyLibrary.functionRequiringCallback(function() {
       this.MyMethod(); //undefined
       this.requiredThirdPartyFunction(); //"this" refers to thirdPartyLibrary
     });
  }
}

I could certainly make the callback an arrow function so that "this" refers to the class-scope, but then I won't have access to "requiredThirdPartyFunction". 我当然可以使回调函数成为一个箭头函数,以便“ this”引用类作用域,但随后我将无法访问“ requiredThirdPartyFunction”。

Any help would be appreciated. 任何帮助,将不胜感激。

When you want a reference to your instance this , you can always use the old that = this assignment. 当您要引用实例this ,可以始终使用旧的that = this分配。 You assign this to a variable in the scope ( that usually), and then you can reference it inside the callback. 你分配this的范围内(一个变量that通常情况下),然后你可以参考它的回调中。

otherMethod() {
  const that = this; // set a reference to the instance this

  thirdPartyLibrary.functionRequiringCallback(function() {
    that.MyMethod(); // that refers to your class instance this
    this.requiredThirdPartyFunction(); //"this" refers to thirdPartyLibrary
  });
}

Another option is to bind myMethod , by using an arrow function or Function#bind, and then assign the bound method to a variable: 另一个选择是通过使用箭头函数或Function#bind绑定myMethod ,然后将绑定的方法分配给变量:

class MyClass {
  myProperty = 'something';

  myMethod = () => console.log(this.myProperty);

  otherMethod() {
     const myMethod = this.myMethod; // assign the bound method to a variable

     thirdPartyLibrary.functionRequiringCallback(function() {
       MyMethod(); // will be invoked with original this
       this.requiredThirdPartyFunction(); //"this" refers to thirdPartyLibrary
     });
  }
}

There are not so many options here. 这里没有太多选择。 Since there is only one this , it can be either lexical: 由于只有this ,因此可以是词汇:

  otherMethod() {
     thirdPartyLibrary.functionRequiringCallback(() => {
       this.MyMethod();
       thirdPartyLibrary.requiredThirdPartyFunction();
     });
  }

Or dynamic: 还是动态的:

  otherMethod() {
     const _this = this;
     thirdPartyLibrary.functionRequiringCallback(function() {
       _this.MyMethod();
       this.requiredThirdPartyFunction();
     });
  }

The first option is more readable and is likely a better choice, since it's not obvious from the code above that this === thirdPartyLibrary inside callback. 第一个选项更具可读性,并且可能是一个更好的选择,因为从上面的代码中不很容易看出回调中的this === thirdPartyLibrary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM