Javascript用子方法覆盖父方法

Fat*_*tas 2 javascript

我怎么能覆盖孩子的父方法?是javascript有像parent :: method();

var Parent = function(){
       this.myMethod(){
          //Some code here
       };
    }

    var Child = function(){
       Parent.call(this);
       this.myMethod(){
          //Some code here
          //and then run parent method
       };
    }
Run Code Online (Sandbox Code Playgroud)

更新有比这更好的方法(不是ES6)?:

var Parent = function ()
{
    this.myMethod()
    {
        this.extedMyMethod()
        //Some code here
    };

    this.extedMyMethod()

}

var Child = function ()
{
    Parent.call(this);
    this.extedMyMethod()
    {
        //Some code which expand parent method
    };
}
Run Code Online (Sandbox Code Playgroud)

PS如果我喜欢@Suren Srapyan suguest webpack将转换为适当的非ES6?

Sur*_*yan 10

使用ES6类语法继承,您可以通过super调用来完成.

class Parent{
   method(){
    console.log('Parent !!!');
   }
}

class Child extends Parent{
  
  constructor(){
     super();
  }

   method(){
    console.log('Child !!!');
    super.method();
   }
}

var child = new Child();
child.method();
Run Code Online (Sandbox Code Playgroud)

更新

您还需要将polyfill用于不同的浏览器