2

dojoで別のメソッドの親メソッドを呼び出すにはどうすればよいですか。次の例を検討してください。

var parent = declare(null,{

m1: function(arg){
console.log("parent.m1");
},
m2: function(arg){
console.log("parent.m2");
}

});`enter code here`

var child = declare(parent,{

m1: function(arg){
console.log("child.m1");
// how can i call parent.**m2** here directly without calling child.m2
},
m2: function(arg){
console.log("child.m2");
}

});

child.m2をまったく呼び出さずに、child.m1から直接parent.m2を呼び出すにはどうすればよいですか。

ここで、2つのモジュールを次のように定義するとします。

parentModule.js

    var parent = declare(null,{

    m1: function(arg){
    console.log("parent.m1");
    },
    m2: function(arg){
    console.log("parent.m2");
    }

    });
    return declare("ParentModule",[parent,child]);
//******************************************//
childModule.js

    return declare("child",null,{

    m1: function(arg){
    console.log("child.m1");
    // how can i call parent.**m2** here directly without calling child.m2
    //if we call ParentModule.prototype.m2.call(this,arguments); this will call child.m2
    //as child module override the parent now
    //also calling this.getInherited("m2",arguments); will call child.m2 !!!
    //how to fix that?
    },
    m2: function(arg){
    console.log("child.m2");
    }

    });
4

2 に答える 2

6

dojoの宣言を使用する場合this.inherited(arguments)、子関数で使用して親関数を呼び出すことができます。以下を参照してください。

http://dojotoolkit.org/reference-guide/1.8/dojo/_base/declare.html#dojo-base-declare-safemixin

m1: function (arg) {
    console.log("child.m1");
    this.inherited(arguments);
}
于 2013-03-06T19:25:34.340 に答える
2

javascriptのプロトタイプ機能を使用して、求めていることを実行できます。

m1: function(arg){
    console.log("child.m1");
    parent.prototype.m2.apply(this, arguments);
},

プロトタイプの詳細については、こちらをご覧ください。JavaScript .prototypeはどのように機能しますか?

これがこの動作の例です http://jsfiddle.net/cswing/f9xLf/

于 2013-03-06T14:47:14.743 に答える