Javascript 继承和方法覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4777522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:03:11  来源:igfitidea点击:

Javascript inheritance and method overriding

javascriptinheritanceprototype

提问by The Coding Monk

Assume I have a class like this:

假设我有一个这样的类:

function Widget() {
    this.id = new Date().getTime();
    // other fields
}
Widget.prototype = {
    load: function(args) {
        // do something
    }
}

From this class I created some other classes which inherit the same prototype but have some added methods. What I want to do is being able to define a load() method in the sub-classes which first calls the parent method and then execute some code. Something like:

从这个类中,我创建了一些其他类,它们继承了相同的原型但添加了一些方法。我想要做的是能够在子类中定义一个 load() 方法,该方法首先调用父方法,然后执行一些代码。就像是:

SpecialWidget.prototype = {
    load: function(args) {
        super.load(args);
        // specific code here
    }
}

I know there's no super keyword in Javascript but there must be a way to do this.

我知道 Javascript 中没有 super 关键字,但必须有办法做到这一点。

回答by karim79

You can simulate it like this:

你可以像这样模拟它:

SpecialWidget.prototype = {
    load: function(args) {
        Widget.prototype.load.call(this, args);
        // specific code here
    }
}

Or you can create your own super property like this:

或者您可以像这样创建自己的超级属性:

SpecialWidget.prototype.parent = Widget.prototype;

SpecialWidget.prototype = {
    load: function(args) {
        this.parent.load.call(this,args);
        // specific code here
    }
}

回答by hvgotcodes

so first, you set up your 'subclass' like so

所以首先,你像这样设置你的“子类”

function SubClass(name) {
    Super.call(this);

    // stuff here
}

SubClass.prototype = new SuperClass(null);
SubClass.prototype.constructor = SubClass;

and then you can do

然后你可以做

SuperClass.prototype.theMethod.apply(this);

from within a subclass implementation to specifically invoke the super's implementation.

从子类实现中专门调用超级的实现。

回答by user113716

I don't know if this is the best solution, but you could do something like this:

我不知道这是否是最好的解决方案,但你可以这样做:

function Widget() {
    this.id = new Date().getTime();
}
Widget.prototype.load = function(args) {
   alert( 'parent load' );
};

SpecialWidget = function(){};

   // Make the prototype of SpecialWidget an instance of Widget
var proto = SpecialWidget.prototype = new Widget;

   // Give the prototype a function that references the "load" from Widget
proto.parent_load = proto.load;

   // Give SpecialWidget its own "load" that first calls the parent_load
proto.load = function( args ) {
    this.parent_load( args );
    alert( 'special load' );
};

var inst = new SpecialWidget;

inst.load();

This makes the prototype of SpecialWidgetan instance of Widgetso that it inherits all that Widgethas.

这使得SpecialWidget实例的原型Widget继承了所有的Widget

Then it makes a reference to the load()of Widgetcalled parent_load(), and creates its own load()that calls the parent_load()when invoked.

然后它引用load()of Widgetcalled parent_load(),并创建自己的load()调用parent_load()when 调用。

回答by david

It would be possible to store the old value of the loadmethod in a closure, if you did your overriding like this:

load如果您像这样进行覆盖,则可以将方法的旧值存储在闭包中:

function Widget() {
    this.id = new Date().getTime();
    // other fields
}

Widget.prototype = {
    load: function(args) {
        // do something
        alert("Widget Prototype Load");
    }
};

function SpecialWidget(){
};

SpecialWidget.prototype = new Widget();

(function(){
    var oldLoad = SpecialWidget.prototype.load;
    SpecialWidget.prototype.load = function(){
        oldLoad();
        alert("new Load");
    };
}());


var x = new SpecialWidget();
x.load();

It works, but I'm not sure if it's the best method.

它有效,但我不确定这是否是最好的方法。

回答by Eduardo Cuomo

Using Simple Javascript Class:

使用简单的 Javascript 类

Class.extend('Widget', {
  load: function () {
    alert('foo');
  }
});

Widget.extend('SpecialWidget', {
  load: function () {
    this.super();
    alert('bar');
  }
});

new Widget().load(); // Alert: 'foo'
new SpecialWidget().load(); // Alert: 'foo' and 'bar'

Take a look at Simple Javascript Class Project, Simple JavaScript Inheritanceand Inheritance Patterns in JavaScript.

查看简单的 Javascript 类项目简单的 JavaScript 继承JavaScript 中的继承模式