简体   繁体   English

每当我调用保存方法时调用保存方法

[英]Calling saving method whenever I call save method

I have a class like this: 我有这样的课:

class X extends Y {
  public save() {

  }
}

abstract class Y {
  public saving() {

  }
}

const x = new X;
x.save();

How can I make sure each call to save triggers saving before? 我怎样才能确保每次调用save触发器saving过吗?

In PHP, we can use __call and check the method name. 在PHP中,我们可以使用__call并检查方法名称。 Is there any similar approach with Javascript? Javascript是否有类似的方法?

First of all define you abstract class Y before extending it. 首先,在扩展它之前定义抽象类Y。 An then you can use super keyword to access base class members. 然后,您可以使用super关键字访问基类成员。 See the code below. 请参见下面的代码。

 abstract class Y { public saving() { console.log("Y Class Saving") } } class X extends Y { public save() { super.saving(); console.log("X Class Save") } } const x = new X(); x.save(); 

Can test the above code here on Playground 可以在Playground上测试以上代码

Hope it helps :) 希望能帮助到你 :)

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

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