これらの方法は、それぞれの長所と短所を心に留めていますか?XMLHttpRequestラッパー/プロキシーを作成するにはどうしたらいいですか?
方法1:ネイティブインスタンスを増大
var _XMLHttpRequest = XMLHttpRequest;
XMLHttpRequest = function() {
var xhr = new _XMLHttpRequest();
// augment/wrap/modify here
var _open = xhr.open;
xhr.open = function() {
// custom stuff
return _open.apply(this, arguments);
}
return xhr;
}
方法2:サブ・ "クラス" は、天然のXMLHttpRequest
var _XMLHttpRequest = XMLHttpRequest;
XMLHttpRequest = function() {
// definePropertys here etc
}
XMLHttpRequest.prototype = new _XMLHttpRequest());
// OR
XMLHttpRequest.prototype = Object.create(_XMLHttpRequest);
// custom wrapped methods on prototype here
XMLHttpRequest.prototype.open = function() {
// custom stuff
return _XMLHttpRequest.prototype.open.apply(this, arguments);
}
方法3:ネイティブのXMLHttpRequestへのフルプロキシ
var _XMLHttpRequest = XMLHttpRequest;
XMLHttpRequest = function() {
this.xhr = new _XMLHttpRequest();
}
// proxy ALL methods/properties
XMLHttpRequest.prototype.open = function() {
// custom stuff
return this.xhr.open.apply(this.xhr, arguments);
}
あなたの最初の方法は間違っているようです。スタックオーバーフローが発生するまでは、再帰的なループが発生する恐れがあります。 ;)そして、なぜこれはとにかくしたいのですか?参照されたオブジェクトに関数呼び出しを提供する必要がない、より洗練されたソリューションがたくさんあります。 –
Marcel:そうです、それはタイプでした、私はそれを修正しました。 – tlrobinson
ThiefMaster:私の質問のほとんどは決して適切に答えられません... – tlrobinson