不要在ES6类中调用继承的方法两次 [英] Don't invoke inherited method twice in ES6 classes

查看:1220
本文介绍了不要在ES6类中调用继承的方法两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从RequireJS转到browserify(与babelify一起),并尝试将我当前的模块重写为类。对于我的每个RequireJS模块,我都有一个名为 eventHandler 的方法来处理所有特定于模块的事件。现在,当我扩展一个类时,父类调用子类的 eventHandler 方法,导致调用该方法两次。



父类:

 'use strict'; 

class Tooltip {
constructor(){
this.eventHandler();
}

eventHandler(){
//模块特定事件
}
}

module.exports = Tooltip;

子类:

 'use strict'; 

从'./Tooltip'导入工具提示;

class Block extends Tooltip {
constructor(){
super();
this.eventHandler();
}

eventHandler(){
//模块特定事件
//获取两次
}
}

module.exports = Block;

我喜欢这样一个事实,即事件Handler方法在所有模块中命名相同,因为它更容易维护。这就是为什么我想保持这种模式。那么解决这个问题最好的办法是什么呢?感谢任何建议!

解决方案

由于你知道父构造函数调用 this.eventHandler ,不要在派生类中这样做:

 'use strict'; 

从'./Tooltip'导入工具提示;

class Block extends Tooltip {
constructor(){
super();
//(No call here)
}

eventHandler(){
//模块特定事件
//获取两次
}
}

module.exports = Block;

重新发表评论:


父类并不总是实现一个eventHandler方法。所以我需要确保它在这种情况下被调用。


他们在做什么,不要打电话。他们不在哪里, do 进行呼叫。子类根据其性质非常紧密地绑定到超类。



如果你可以假设(!)存在 eventHandler 在超级意味着它从它的构造函数中调用它,你可以这样做:

  constructor(){
super();
if(!super.eventHandler){
this.eventHandler();
}
}

...但再次,超级的性质/ sub关系是它是非常紧密的,所以依靠你对超级是否这样做的了解是合理的。


I'm moving from RequireJS to browserify (together with babelify) and try to rewrite my current modules to classes. For each of my RequireJS modules I have a method called eventHandler which handles all module specific events. Now when I extend a class, the parent class calls the subclass`s eventHandler method which leads to invoking the method twice.

Parent class:

'use strict';

class Tooltip {
    constructor() {
        this.eventHandler();
    }

    eventHandler() {
        // Module specific events
    }
}

module.exports = Tooltip;

Subclass:

'use strict';

import Tooltip  from './Tooltip';

class Block extends Tooltip {
    constructor() {
        super();
        this.eventHandler();
    }

    eventHandler() {
        // Module specific events
        // Gets called twice
    }
}

module.exports = Block;

I liked the fact that the eventHandler method was named the same across all modules as it was easier to maintain. That's why I'd like to keep this pattern. So what would be the best way to solve this problem? Thanks for any suggestions!

解决方案

Since you know the parent constructor calls this.eventHandler, don't do so in the derived classes:

'use strict';

import Tooltip  from './Tooltip';

class Block extends Tooltip {
    constructor() {
        super();
        // (No call here)
    }

    eventHandler() {
        // Module specific events
        // Gets called twice
    }
}

module.exports = Block;

Re your comment:

The parent classes don't always implement an eventHandler method. So I need to ensure it gets called in this case as well.

Where they do, don't do the call. Where they don't, do do the call. Subclasses are very tightly bound to superclasses, by their nature.

If you can assume (!) that the presence of eventHandler in the super means it has called it from its constructor, you could do something like this:

constructor() {
    super();
    if (!super.eventHandler) {
        this.eventHandler();
    }
}

...but again, the nature of the super/sub relationship is that it's very tightly-bound, so relying on your knowledge of whether the super does this or not is reasonable.

这篇关于不要在ES6类中调用继承的方法两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆