Javascript:防止从父函数执行子类函数

Javascript:防止从父函数执行子类函数,javascript,javascript-objects,Javascript,Javascript Objects,父类: clicked:function(){ if(!this.enabled) return; }, 子覆盖父函数: clicked:function(){ this.parent(); console.log('Clicked'); } 我试图阻止使用上述代码禁用子函数时执行子函数,但它不起作用,父函数只是停止自身,子函数继续执行。是否可以使父级停止任何要执行的重写代码?谢谢 更新: 假设我有50个子类,它们继承自同一个类。有没有更简单的方法,我们不需要: if

父类:

clicked:function(){
    if(!this.enabled) return;
},
子覆盖父函数:

clicked:function(){
    this.parent();
    console.log('Clicked');
}
我试图阻止使用上述代码禁用子函数时执行子函数,但它不起作用,父函数只是停止自身,子函数继续执行。是否可以使父级停止任何要执行的重写代码?谢谢

更新: 假设我有50个子类,它们继承自同一个类。有没有更简单的方法,我们不需要:

if(!this.enabled) return;
this.parent();

在每个子类中单击函数?

在父对象中,必须有一个函数来检查父对象是否已启用。父函数的clicked函数应该负责执行一些操作

clicked:function(){
    return this.enabled;
} 

clicked:function(){
    if (this.parent()) console.log('Clicked');
}
enabled: function() {
    return this.enabled;
}
clicked:function() {
    // this must be responsible for invoking some action
    // do some action
} 
在子对象中,必须检查父对象是否已启用。(这就是你想要达到的目标——猜测)


Javascript继承有时有点困难。这里有一个小例子

// parent class constructor
MMN.Parent = function(text) {
    this.member = text;
}

MMN.Parent.prototype = {
    setMember : function(text) {
        this.member = text;
    }
}

// child class constructor
MMN.Child = function() {
    MMN.Parent.call(this, 'This text is set from the child constructor');
}

// inheritance
MMN.Child.prototype = Object.create(MMN.Parent.prototype);

// override and call of parent method
MMN.Child.prototype.setMember = function(text) {
    MMN.Parent.prototype.setMember(text);
    console.log('this logs from the child method');
}

此示例显示了如何在子类中调用父方法。

能否提供更多上下文?
// parent class constructor
MMN.Parent = function(text) {
    this.member = text;
}

MMN.Parent.prototype = {
    setMember : function(text) {
        this.member = text;
    }
}

// child class constructor
MMN.Child = function() {
    MMN.Parent.call(this, 'This text is set from the child constructor');
}

// inheritance
MMN.Child.prototype = Object.create(MMN.Parent.prototype);

// override and call of parent method
MMN.Child.prototype.setMember = function(text) {
    MMN.Parent.prototype.setMember(text);
    console.log('this logs from the child method');
}