0

私は次のようなjsコ​​ードを持っています、

   function findDealerByIdResponse() {
findDealerByIdResponse.prototype = findDealerByIdResponseType;


this.getPrefix = function(){
        return "tns1";
};
this.getNS = function(){
    return "http://www.hp.com/bookservice";
};

} 

function findDealerByIdResponseType(){
var DEALER ;


this.getPrefix = function(){
        return "tns1";
};
this.getNS = function(){
    return "http://www.hp.com/bookservice";
};


}

function getName( obj ) { 

   var funcNameRegex = /function (.{1,})\(/;
   var results = new Object();
   results = (funcNameRegex).exec((obj).constructor.toString());
   return (results && results.length > 1) ? results[1] : "";
};

function test(){
var req = new findDealerByIdResponse();
alert(getName(req));
}

しかし、初めて「テスト」を実行すると、期待どおりの結果が得られます:「doTransactionType」。しかしその後、それは「機能」を与えています。その理由を説明してください。

ありがとう

ディーパック

4

1 に答える 1

0

の最初の呼び出しの後、findDealerByIdResponseそのprototypeプロパティを関数、つまり。に設定しますfindDealerByIdResponseType。その結果、.constructorはこの割り当てによって上書きされ、その関数のコンストラクターに解決されます。これは、他の関数と同様に、Functionです。

代わりに、これが必要です。

// set prototype to a parent instance
findDealerByIdResponse.prototype = new findDealerByIdResponseType;

// restore .constructor
findDealerByIdResponse.prototype.constructor = findDealerByIdResponse;

両方の関数の宣言の後に配置することをお勧めします。

于 2012-06-05T09:37:51.840 に答える