Ich bin neu in JavaScript. Neu, soweit ich es wirklich getan habe, ist der vorhandene Code optimiert und kleine Teile von jQuery geschrieben.
Jetzt versuche ich, eine "Klasse" mit Attributen und Methoden zu schreiben, aber ich habe Probleme mit den Methoden. Mein Code:
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
Das Problem ist, in Request.prototype.start
, this
ist nicht definiert und damit die if - Anweisung das Ergebnis falsch. Was mache ich hier falsch?
start
in der habenprototype
?