2016-08-31 5 views
-1

親クラスから実行される子クラスの機能を防ぐ:Javascriptを:親関数

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

子オーバーライド親関数:

clicked:function(){ 
    this.parent(); 
    console.log('Clicked'); 
} 

私は無効になったときに実行されるように、子供の機能を阻止しようとしています上記のコードを使用していますが動作しません、親関数はちょうど自身を停止し、子は実行を続けます。親が実行されるオーバーライドコードを停止させることは可能ですか?ありがとう。

更新: 私は同じクラスから継承する50の子クラスを持っているとしましょう。簡単な方法はありますか?

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

各子クラスでクリックした機能はありますか?

+1

もっと文脈を提供できますか? – MicronXD

答えて

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

clicked:function(){ 
    if (this.parent()) console.log('Clicked'); 
} 
0

親オブジェクトには、親が有効かどうかをチェックする機能が必要です。親のクリックされた機能は、何らかの処置を行う責任を負うべきである。

enabled: function() { 
    return this.enabled; 
} 
clicked:function() { 
    // this must be responsible for invoking some action 
    // do some action 
} 

子オブジェクトでは、親が有効かどうかをチェックする必要があります。 (これはあなたが達成しようとしているものは推測です)

clicked:function() { 
    if (this.enabled()) console.log('Clicked'); 
} 
0

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'); 
} 

この例では、子クラスで親メソッドを呼び出す方法を示します。

関連する問題