0

だから私はクラスを作成しました

function myClass()
{
}

メソッドで

myClass.prototype.theMethod=function()
{
}

どちらで問題ありませんが、可能であれば、このクラスを使用する必要がありますが、すべてを上書きするのではなく、メソッドにコマンドを追加する必要がありますか?

4

2 に答える 2

0

特定のオブジェクトでアクションをオーバーライドする必要がある場合は、次のことができます。

var myInstance = new myClass();

myInstance.theMethod = function () {
    // do additional stuff
    // now call parent method:
    return myClass.prototype.theMethod.apply(this, arguments);
}

サブクラスの解決策はほとんど同じですが、インスタンスではなく、継承された「クラス」のプロトタイプで実行します。

于 2013-09-06T10:35:26.703 に答える
0

このような:

var theOldMethod = myClass.prototype.theMethod;
myClass.prototype.theMethod=function()
{
    //Do stuff here
    var result = theOldMethod.apply(this, arguments);
    //Or here
    return result;
}
于 2013-09-06T10:38:11.977 に答える