简体   繁体   English

如何在匿名 function 中访问 class 方法

[英]How to access class method inside an anonymous function

I have an method in my JS class and in the callback of a Promise, I want it to call another class method.我的 JS class 中有一个方法,在 Promise 的回调中,我希望它调用另一个 class 方法。

class MyClass {
   myClassMethod(arg1) {
         // this method did get called
   }
   
   aSecondClassMethod() {
          //...
   }

   methodWithPromise() {
       var myClassMethod = this.myClassMethod;
       let aPromise = methodReturnPromise(); 
       aPromise.then(function (value) {
           myClassMethod(value);
   }
 }

So I create a var calls myClassMethod and set that to this.myClassMethod .所以我创建了一个 var 调用myClassMethod并将其设置为this.myClassMethod And when I debug the code, myClassMethod did get called in the then callback of the Promise.当我调试代码时,myClassMethod 确实在 Promise 的then回调中被调用。

The problem I am having is when my myClassMethod() calls other class method(), ie我遇到的问题是当我的myClassMethod()调用其他 class 方法() 时,即

myClassMethod(args) {
  aSecondClassMethod();
}

I get error saying aSecondClassMethod is undefined.我收到错误消息说aSecondClassMethod未定义。 I tried我试过了

myClassMethod(args) {
  this.aSecondClassMethod();
}

But it gives me the same error.但它给了我同样的错误。 I think I can work around this by declaring a var for each of the class method that myClassMethod() calls.我想我可以通过为 myClassMethod() 调用的每个 class 方法声明一个 var 来解决这个问题。

   var aSecondClassMethod= this.aSecondClassMethod;

But that seem cumbersome to maintain the code going forward.但这对于继续维护代码似乎很麻烦。

I would like to know if there is a better way to do this.我想知道是否有更好的方法来做到这一点。

Use an arrow function, as it captures the this value of the enclosing context.使用箭头 function,因为它捕获封闭上下文的this值。

aPromise.then(value => this.myClassMethod(value));

I would also recommend using the new () => {} function notation for defining the class method that contains the promise. Without that (or an old school bind) this will still be undefined .我还建议使用新的() => {} function 符号来定义包含 promise 的 class 方法。如果没有它(或老式绑定) this仍然是undefined

My usual style:我一贯的作风:

class MyClass {

  myClassMethod = (value) => {
    //something..
  };

  methodWithPromise = () => {
    somePromise
      .then((res) => {
        this.myClassMethod(res);
      })
      .catch((err) => {
        console.error(err);
        return;
      });
  };
}

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

相关问题 在原型方法内访问匿名 function 内的实例变量 - Access an instance variable inside an anonymous function inside prototype method 如何访问 JavaScript 匿名函数中的变量 - How to access variable inside JavaScript anonymous function 您如何访问匿名函数内部的javascript类属性是同一类 - How can you access a javascript class property inside of an anonymous function is the same class 如何在内部生成器函数内部访问超类方法? - How to access super class method inside of internal generator function? 如何从外部访问匿名函数内部的函数 - How do you access a function inside an anonymous function from outside jQuery:如何从匿名函数内部访问父函数“this”? - jQuery: how to access parent function “this” from inside anonymous function? 如何在匿名函数/闭包中动态访问变量? - How to access variable dynamically inside an anonymous function/closure? 如何调用“定义”内部的JavaScript函数 - 匿名方法 - How to call a JavaScript function that is inside a “define” - anonymous method 如何从JavaScript中的匿名函数内部访问方法变量? - How to access method variables from within an anonymous function in JavaScript? 如何将匿名函数变量分配给类方法变量? - How to assign anonymous function variables to class method variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM