简体   繁体   English

如何在JavaScript中调用静态子类方法

[英]How to call a static subclass method in JavaScript

How can I call a static method of a subclass from a static method of the parent class? 如何从父类的静态方法调用子类的静态方法?

class A {

  static foo(){
    // call subclass static method bar()
  }
}

class B extends A {

  static bar(){
    // do something
  }
}

B.foo()

Update : 更新

The reason why I tried this is that subclasses of A would have worked best as singletons in my context and I wanted to use the template method pattern in A. 我尝试这个的原因是A的子类在我的上下文中最适合作为单例,我想在A中使用模板方法模式

Since it looks like I cannot get a reference to a subclass from a static context I am now exporting instances of subclasses of A which works just as well. 因为看起来我无法从静态上下文中获取对子类的引用,所以我现在正在导出A的子类的实例,它们也可以正常工作。 Thanks. 谢谢。

Update 2 更新2

Yes, it is a duplicate to a degree (the other question does not involve subclassing). 是的,它是一个程度的重复(另一个问题不涉及子类)。 The reference, even from a static context, is this . 即使从静态上下文,引用也是this So this works: 这样可行:

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

I am a bit confused to your need, as it seems you sorta already get what you need to do with B.foo(). 我对你的需求有点困惑,因为你似乎已经得到了你需要用B.foo()做的事情。 So, is this what you need? 那么,这就是你需要的吗?

class A {

  static foo(){
    // call subclass static method bar()
    // Because it is "static" you reference it by just calling it via
    // the class w/o instantiating it.
    B.bar() 

  }
}

class B extends A {

  static bar(){
    // do something
    console.log("I am a static method being called")
  }
}

// because "foo" is static you can call it directly off of 
// the class, like you are doing
B.foo()

// or
var D = new B()
D.bar() // won't work ERROR
A.foo() // Works <-- Is this is specifically what you are asking? Or 
        // calling it in the Super class like B.bar(), that is done in static method foo?

Is this what you are asking? 这是你要问的吗? If this does not answer your question, please let me know what I am misunderstanding and I'll try to answer. 如果这不能回答你的问题,请告诉我我的误解,我会尽力回答。 Thanks. 谢谢。

It makes the template pattern somewhat less elegant, but you can accomplish this via super in the subclass, eg: 它使模板模式稍微不那么优雅,但你可以通过子类中的super来实现这一点,例如:

class A {
  static foo() {
    this.bar()
  }
}

class B extends A {
  static foo() {
    super.foo()
  }
  static bar() {
    console.log("Hello from B")
  }
}

B.foo()

暂无
暂无

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

相关问题 Javascript子类中的实例方法可以将其称为父方法静态方法吗? - Can a instance method in a Javascript subclass call it's parents static method? Javascript ES6:如何从超类中定义的静态方法中检索调用子类 - Javascript ES6: How to retrieve calling subclass from a static method defined in superclass 如何从 Blazor ZD7EFA19FBE7D3972FD74ZZ 代码调用 static JavaScript 方法 - How to call a static JavaScript method from Blazor C# code 如何从JavaScript的子类中调用父类的方法,以便可以访问父类的局部变量? - How to call parent class' method from a subclass in JavaScript so that parent's local variables would be accessible? 在Javascript中的类的其他静态方法中调用静态方法 - Call a static method inside other static method of a class in Javascript 如何在基类方法中调用子类方法? - How do you call a subclass method in the baseclass method? JavaScript static class 名称和方法名称都是字符串时,如何调用一个方法? - How to call a JavaScript static class method when the class name and method name are strings? 如何在静态方法中调用实例方法? - how to call an instance method inside a static method? JavaScript:如何在超类上调用 STATIC 方法,但该方法未在派生的 class 上定义? - JavaScript: How to call STATIC method on superclass but the method isnot defined on the derivated class? 如何正确地子类化JavaScript中的子类? - How to properly subclass a subclass in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM