【问题标题】:Changing parent's class static method from a subclass从子类更改父类的静态方法
【发布时间】:2019-07-28 21:26:09
【问题描述】:

我需要从子类中更改父类的静态方法。

来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static 我读到:

静态方法调用直接在类上进行,而不是 可在类的实例上调用。

在下面的示例中,我有一个带有 foo() 方法的 Parent 类,它调用了 bar() 方法(都是静态的)。我需要将 barChild 子类更改为调用 Child.foo() 将调用修改后的 bar 方法,而不是原来的方法。

有没有可能(可能是孩子的constructor)?

class Parent {

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

  static bar() {
    console.log("HERE I AM");
  }

}

class Child extends Parent {

  static bar() {
    super.bar(); // maybe not what I want?
    console.log(", FELLAS!");
  }
}

Parent.foo(); // HERE I AM
Child.foo(); // HERE I AM, FELLAS! (need this!)

【问题讨论】:

  • 好吧,这就是问题所在,我需要打电话给Child.foo(),然后苦苦思考如何让它发挥作用……将在问题中添加此信息。
  • 为什么有foo()?调用 bar() 反而会给你你想要的行为。 Parent.bar() 输出“我在这儿”,Child.bar() 输出“我在这儿\n,伙计们!”。
  • @Tibrogargan 我正在尝试实现在the code of another person 中实现并从另一个(多个)方法调用的方法(RenderLayer_())。
  • @Paulpro 我试图在我之前的评论中解释原因。我只是想知道这是否可行。我尝试这个的原因并不是那么简单,但主要是因为我想要一种简单的方法来扩展一个必须由同一类的另一个方法调用的类的静态方法。
  • 不要投反对票...我认为问题很清楚。如果这不可行,我更喜欢有人告诉我,而不是没有任何理由的默默否决。

标签: javascript inheritance ecmascript-6 es6-class


【解决方案1】:

你的问题是foo直接调用Parent.bar(),而不是this.bar()。通过显式引用Parent,它根本不考虑Child 中的覆盖方法。 Child.bar 的写法和是否调用 super.bar 无关紧要。

class Parent {
  static foo() {
    this.bar();
//  ^^^^
  }
  static bar() {
    return "HERE I AM";
  }
}

class Child extends Parent {
  static bar() {
    return super.bar() + ", FELLAS!";
  }
}

console.log(Parent.foo()); // HERE I AM
console.log(Child.foo()); // HERE I AM, FELLAS!

static bar() 方法中的this 关键字现在引用Child.foo() 调用中的Child 类,并调用其覆盖的bar 方法。

唯一的替代方法(如果您无法修改 Parent)也将覆盖 foo 方法,复制 Parent 代码但在那里显式调用 Child.bar()

【讨论】:

  • 感谢您添加“唯一的选择”部分,它阐明了这一点。
  • *添加不添加(来自移动设备)
【解决方案2】:

如果您想从孩子修改父级,请执行此操作。 Parent 是一个具有属性的对象,static 是语法糖。替换方法就像设置属性一样简单。可能不是您想在生产代码中做的事情。

class Parent {
  static foo() {
    Parent.bar();
  }

  static bar() {
    console.log("HERE I AM");
  }
}

class Child extends Parent {
  static unhack() {
      if (Parent.hack) {
          Parent.bar = Parent.hack
          delete Parent.hack
      }
  }
  
  static hack() {
    if (!Parent.hack) {
       Parent.hack = Parent.bar
       Parent.bar = Child.bar
    }
  }
 
  static foo() {
      Child.hack()
      Parent.foo()
      Child.unhack()
  }

  static bar() {
    if (super.hack) {
      super.hack(); // Call the "shadowed" Parent.bar()
    }
    console.log(", FELLAS!"); // Call the additional code
  }
}

Parent.foo(); // HERE I AM
Child.foo(); // HERE I AM, FELLAS! (need this!)
Parent.bar(); // HERE I AM
Child.bar(); // Probably don't want to do this

【讨论】:

  • 看来是我需要的!明天我可以测试它时会回来找你。谢谢,很抱歉现在没来……
  • 是的,成功了,尽管我想我可能会按照许多(和你的)cmets 走另一条路线。谢谢!
  • 为什么要用hackunhack 做这些复杂的事情?只是不要从Child.foo() 拨打Parent.foo(),你不会有任何问题。
  • @bergi 可能已经对 OP 到底在做什么得出结论,但是是的,从表面上看,Parent.foo(); Child.bar() 实现了相同的结果,而没有乱七八糟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多