Sono nuovo di JavaScript. Nuovo per quanto ne ho fatto veramente, è stato ottimizzato il codice esistente e scritto piccoli bit di jQuery.'questo' non è definito nei metodi della classe JavaScript
Ora sto tentando di scrivere una "classe" con attributi e metodi, ma ho problemi con i metodi. Il mio codice:
function Request(destination, stay_open) {
this.state = "ready";
this.xhr = null;
this.destination = destination;
this.stay_open = stay_open;
this.open = function(data) {
this.xhr = $.ajax({
url: destination,
success: this.handle_response,
error: this.handle_failure,
timeout: 100000000,
data: data,
dataType: 'json',
});
};
/* snip... */
}
Request.prototype.start = function() {
if(this.stay_open == true) {
this.open({msg: 'listen'});
} else {
}
};
//all console.log's omitted
Il problema è, in Request.prototype.start
, this
è indefinito e quindi l'istruzione if restituisce false. Cosa sto facendo di sbagliato qui?
C'è un motivo per cui hai 'start' nel' prototipo'? – xj9
A cosa serve 'Request.prototype'? –
Non ho idea: S –