简体   繁体   English

Javascript“扩展”子对象或子类中的方法

[英]Javascript “extending” a method in a sub-object or subclass

My goal is to have a widget object and have specific widgets extend that object. 我的目标是拥有一个小部件对象,并让特定的小部件扩展该对象。 I would like to prevent people from having to call the render callback explicitly in every widget definition by somehow extending it in the parent (Widget). 我想通过以某种方式在父级(小部件)中扩展它来防止人们不得不在每个小部件定义中显式调用渲染回调。 What is the best way to accomplish this? 做到这一点的最佳方法是什么?

 var Widget = function (options) {

    // super class

    if (this.constructor === Widget) {
        throw new Error("You can't instantiate abstract class.");
    }

    var widget = this.__proto__;

    Widget.renderCallBack = function(){
        //alert('the widget has rendered');
    }

    this.render = (function(){
        var _render = this.render;

        return function(){
            _render();
            Widget.renderCallBack();
        }

    })


}
Widget.constructor = Widget;


var SpecialWidget = function(options) {
    Widget.apply(this, arguments);
}

SpecialWidget.prototype = Object.create(Widget);
SpecialWidget.constructor = SpecialWidget

SpecialWidget.prototype.render = function(){
    // render something
}

new SpecialWidget(options);

In addition to my comment: If it is the same for all it doesn't have to be a callback function, just put the code to the end of the render method if it's job is synchronous, if not use a promise but you have to think about that of method shadowing you will overwrite the parent class' render method in the child. 除了我的评论:如果所有内容都相同,则不必是回调函数,如果它的工作是同步的,则只需将代码放在render方法的末尾,如果不使用promise,则必须考虑一下方法阴影,您将覆盖子级中父类的render方法。 To avoid this you can use different name for the methods and call the parent's method from the child or use ES2015 class which has the super keyword for reaching the parent class' methods. 为避免这种情况,您可以为方法使用不同的名称,并从子级中调用父方法,或使用ES2015类,该类具有用于到达父类方法的super关键字。

Also you had a typo in your code: 您的代码中也有错别字:

SpecialWidget.prototype = Object.create(Widget);

It has to be: 它一定要是:

SpecialWidget.prototype = Object.create(Widget.prototype);

Anyway I don't think it is a good idea in general. 无论如何,我一般认为这不是一个好主意。

 var Widget = function(options) { this.options = options || {}; }; Widget.prototype.widgetRender = function() { // do stuff console.log('Widget render'); // callback functionality console.log('Widget render callback functionality'); }; Widget.constructor = Widget; var SpecialWidget = function(options) { Widget.apply(this, arguments); }; SpecialWidget.prototype = Object.create(Widget.prototype); SpecialWidget.prototype.render = function() { // parent's render this.widgetRender(); // do stuff console.log('SpecialWidget render'); // callback functionality console.log('SpecialWidget render callback functionality'); }; SpecialWidget.constructor = SpecialWidget var sw = new SpecialWidget(); sw.render(); // ES2015 class ES6Widget { constructor(options) { this.options = options || {}; } render() { // do stuff console.log('ES6Widget render'); // callback functionality console.log('ES6Widget render callback functionality'); } } class ES6SpecialWidget extends ES6Widget { render() { // parent's render super.render(); // do stuff console.log('ES6SpecialWidget render'); // callback functionality console.log('ES6SpecialWidget render callback functionality'); } } const es6sw = new ES6SpecialWidget(); es6sw.render(); 

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

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