Questi metodi che vengono in mente, quali sono i pro e i contro di ciascuno?Come posso creare un wrapper/proxy XMLHttpRequest?
Metodo 1: Aumentare esempio nativo
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;
}
Metodo 2: Sub "classe" XMLHttpRequest nativo
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);
}
Metodo 3: delega completo a XMLHttpRequest nativo
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);
}
Il tuo primo metodo mi sembra sbagliato. Temo che entrerai in un ciclo ricorsivo fino a ottenere un overflow dello stack. ;) E perché lo vuoi comunque? Esistono molte soluzioni più ordinate, che non devono fornire chiamate di funzione a un oggetto di riferimento. –
Marcel: hai ragione, era un tipo, l'ho risolto. – tlrobinson
ThiefMaster: la maggior parte delle mie domande non ha mai una risposta adeguata ... – tlrobinson