简体   繁体   English

Javascript ES6(ES2015)/ Typescript:如何在静态方法中访问本地方法

[英]Javascript ES6(ES2015)/ Typescript: how to access local method inside static method

I basically want to access the "foo2" method inside a static "bar" method, but so far I only able access "foo1" and "foo3" method only. 我基本上想访问静态“ bar”方法内的“ foo2”方法,但到目前为止,我只能访问“ foo1”和“ foo3”方法。 Can anyone teach me how can I access the non static foo2 method. 谁能教我如何访问非静态foo2方法。

let foo1 = () => {
    alert('foo1’);
  };

class ABC extends Component {

  static bar() {
      foo1(); //can work

      foo2; //not work with error "Can't find variable foo2"

      foo2(); //not work with error "Can't find variable foo2"

      this.foo2(); //not work with error "this.foo2 is not a function"

      this.foo2; //no error but no alert, basically nothing happen

      ABC.foo3() //can work
  }

  foo2 = () => {
      alert('foo2');
  };

  static foo3 = () => {
      alert('foo3');
  };

}

module.exports = ABC;

WARNING: accessing instance-methods from static-methods is not desired, it might confuse other developers. 警告:不需要从静态方法访问实例方法,这可能会使其他开发人员感到困惑。 Try to avoid using it. 尽量避免使用它。 If you do use it - make sure your code is well commented and explains how does it work. 如果您确实使用它-请确保您的代码注释正确并说明其工作方式。

What you want to do is to bind the static method with context of the object which method you want to invoke from static method. 您要做的是将静态方法与对象的上下文绑定,该对象要从静态方法中调用。 You can do this by invoking it with call method: 您可以通过使用call方法调用它来做到这一点:

class ABC extends Component {
    static bar () {
      this.foo();
    }
    foo () {
      // function body
    }
}

let instance = new ABC();
ABC.bar.call(instance);

or using bind if you want to pass the function like callback, etc, pre-bind it, so to speak: 或使用bind如果您想传递回调等函数,则将其预绑定 ,可以这么说:

let instance = new ABC();
setTimeout(ABC.bar.bind(instance), 1000);

NOTE: this by default references your class, not instance of the class (it will reference an instance after we bind it), so if you want to call another static method from the first static method you want to use full class name. 注意:默认情况下, this引用您的类,而不是该类的实例(在我们绑定该实例后,它将引用一个实例),因此,如果要从第一个静态方法中调用另一个静态方法,则要使用完整的类名。

static bar () {
  this.foo();
  ABC.someOtherStaticMethod();
}

ALSO NOTE: if you use Typescript you will get an error when referencing instance-method, because this in your static method by default reference your class. 还请注意:如果您使用Typescript,则在引用实例方法时会出现错误,因为默认情况下, this方法在您的静态方法中引用您的类。 To work-around this you might want cast this's type to any : 要解决此问题,您可能需要将此类型转换为any

static bar () {
  (<any>this).foo();
}

Here you tell Typescript compiler something like "presume that this is not our class but something we don't know" just to get rid of error on compile time. 在这里,您告诉Typescript编译器类似“假定this不是我们的类,但我们不知道的东西”之类的东西,只是为了消除编译时的错误。

You can never access instance methods from a static method (in any language). 您永远不能从静态方法(任何语言)访问实例方法。 There is only one static method, but multiple instances, how would it know which instance ( this ) to use? 只有一个静态方法,但是有多个实例,它如何知道要使用哪个实例( this )?

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

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