Javascript 从同一个类中的另一个方法调用一个方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43642729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:04:01  来源:igfitidea点击:

Calling a method from another method in the same class

javascriptecmascript-6

提问by steve-o

Why am I getting the error: "Uncaught TypeError: self.myTest is not a function"? How do I call a method from within another method in a javascript class?

为什么我收到错误:“未捕获的类型错误:self.myTest 不是函数”?如何从 javascript 类中的另一个方法中调用一个方法?

class MyClass {

    myTest() {
      console.log('it works');
    }

    runMyTest() {
      self.myTest();
    }

}

var myClass = new MyClass();
myClass.runMyTest();

采纳答案by Toby Mellor

You need to use the thiskeyword instead of self.

您需要使用this关键字而不是self.

runMyTest() {
    this.myTest();
}

A side note

旁注

If you are nesting standard functions notation then thisis not lexically bound (will be undefined). To get around this, use Arrow Functions (preferred), .bind, or locally define thisoutside of the function.

如果您正在嵌套标准函数表示法,则this不受词法约束(将是未定义的)。要解决此问题,请使用箭头函数(首选).bind、 或在this函数外部局部定义。

class Test {
  constructor() {
    this.number = 3;
  }

  test() {
    function getFirstThis() {
       return this;
    }

    const getSecondThis = () => {
       return this;
    };

    const getThirdThis = getFirstThis.bind(this);
    
    const $this = this;
    function getFourthThis() {
      return $this;
    }

    // undefined
    console.log(getFirstThis());
    
    // All return "this" context, containing the number property
    console.log(this); 
    console.log(getSecondThis());
    console.log(getThirdThis());
    console.log(getFourthThis());
  }
}

new Test().test();

回答by edi

You need to use thisnot selflike

你需要使用thisself喜欢

runMyTest() {
  this.myTest();
}

However a lot of implementations like to keep the reference and are doing the following:

然而,许多实现喜欢保留引用并执行以下操作:

var self = this;

That might be the reason you were thinking of selfas self reference. For further reading I'd suggest this SO - post

这可能是您认为self是自我参考的原因。如需进一步阅读,我建议使用此SO - post

回答by x-rw

Other solution is save the value of variable context $thisinside other for example in this

其他解决方案是将变量上下文的值保存$this在其他内部,例如this

and for use is this.anyFunction();

和使用是 this.anyFunction();

class MyClass {
    myTest() {
      console.log('it works');
    }

    runMyTest() {
      let this=$this;
      this.myTest();
    }
}

回答by Salah Eddib

class MyClass {

    myTest() {
      console.log('it works');
    }

    runMyTest() {
      this.myTest();
    }

}

var myClass = new MyClass();
myClass.runMyTest();

回答by M Melnikov

class MyClass {

    myTest() {
      console.log('it works');
    }

    runMyTest() {
      this.myTest();
    }

}

var myClass = new MyClass();
myClass.runMyTest();