fwolle: Problem mit Objekten und JavaScript klassen

Guten Abend, ich hab ein kleines Problm mit folgender JavaScript klasse:

  
  
function AjaxRequest (url, params)  
{  
 this.url = url;  
 this.is_async=params.is_async;  
 this.method = (params.method)?params.method.toUpperCase():"GET";  
 this.parameter = (params.parameter)?params.parameter:null;  
 if (params.onSuccess)  
  this.onSuccess=params.onSuccess;  
 else {  
  alert("Keine onSuccess Ereignissfunktion angegeben.\n\nBitte geben Sie einen Funktion an, die beim onSuccess ausgeführt.");  
  return false;  
 }  
 this.onWaiting=(params.onWaiting)?params.onWaiting:this.onSuccess;  
 this.onFailure=(params.onFailure)?params.onFailure:this.onSuccess;  
  
 this.anfrage=null;  
 try {  
  this.anfrage=new XMLHttpRequest();  
 } catch(versuchmicrosoft) {  
  try {  
   this.anfrage=new ActiveXObject("Msxml2.XMLHTTP");  
  }catch(anderesmicrosoft) {  
   try {  
    this.anfrage=new ActiveXObject("Microsoft.XMLHTTP");  
   } catch(fehlschlag) {  
    this.anfrage=null;  
   }  
  }  
 }  
 if (!this.anfrage) {  
  alert ("Das Anfrageobjekt konnte nicht initialisiert werden.\n\nBitte versuchen Sie es mit einen Anderen Browser.");  
  return false;  
 }  
  
 this.checkState = function() {  
  if (this.readyState==2) {  
   anfrage.onWaiting();  
  } else if (this.readyState==4 && this.status==200) {  
   anfrage.onSuccess();  
  } else if (this.readyState==4 && this.status!=200) {  
   anfrage.onFailure();  
  }  
 }  
  
 this.sendRequest = function() {  
  var parameter=this.parameter;  
  if (this.method=="GET"){  
   this.anfrage.open("GET",this.is_async,this.url + "?" + parameter);  
   this.anfrage.onreadystatechange = this.checkState;  
   this.anfrage.send(null);  
  }  
 };  
}  
  
var anfrage = new AjaxRequest("http://localhost/kapitel01/boards/aktuelleBoardVerkaeufe.php",{  
  method:"GET",  
  parameter:"wort=test",  
  is_async: true,  
  onWaiting:test,  
  onSuccess:test,  
  onFailure:test  
 });  
  
function test() {alert ("test");}  
  
anfrage.sendRequest();  

Die klassen funktioniert zwar, nur möchte ich sie algemein gültig machen, sprich bei this.checkState soll anstatt "anfrage.onError()" ein ausdruck stehen, der die funktion onError aus der Klasse aufruft. ich hab mit "this.onError()" versucht, aber leider ist in this das Abfrageobjekt gespeichert. also nun meine Frage : wie kriege ich das klassenobjekt herraus, ohne anfrage zu nutzen?

  1. Hi,

    Die klassen funktioniert zwar, nur möchte ich sie algemein gültig machen, sprich bei this.checkState soll anstatt "anfrage.onError()" ein ausdruck stehen, der die funktion onError aus der Klasse aufruft. ich hab mit "this.onError()" versucht, aber leider ist in this das Abfrageobjekt gespeichert. also nun meine Frage : wie kriege ich das klassenobjekt herraus, ohne anfrage zu nutzen?

    http://aktuell.de.selfhtml.org/artikel/javascript/organisation/#closures-anwendung

    MfG ChrisB

    --
    „This is the author's opinion, not necessarily that of Starbucks.“
    1. Klasse, Funktioniert gut, sehr gut, danke. ich habe zwar in google gesucht, leider wusste ich nicht genau, wonach ich suchen sollte. aber vielen dank für deine Hilfe.

      Für die jenigen, die ein ähnliches problem haben, hier die veränderte version :

        
        
      function AjaxRequest (url, params)  
      {  
       var thisObject=this;  
       this.url = url;  
       this.is_async=params.is_async;  
       this.method = (params.method)?params.method.toUpperCase():"GET";  
       this.parameter = (params.parameter)?params.parameter:null;  
       if (params.onSuccess)  
        this.onSuccess=params.onSuccess;  
       else {  
        alert("Keine onSuccess Ereignissfunktion angegeben.\n\nBitte geben Sie einen Funktion an, die beim onSuccess ausgeführt.");  
        return false;  
       }  
       this.onWaiting=(params.onWaiting)?params.onWaiting:this.onSuccess;  
       this.onFailure=(params.onFailure)?params.onFailure:this.onSuccess;  
        
       this.anfrage=null;  
       try {  
        this.anfrage=new XMLHttpRequest();  
       } catch(versuchmicrosoft) {  
        try {  
         this.anfrage=new ActiveXObject("Msxml2.XMLHTTP");  
        }catch(anderesmicrosoft) {  
         try {  
          this.anfrage=new ActiveXObject("Microsoft.XMLHTTP");  
         } catch(fehlschlag) {  
          this.anfrage=null;  
         }  
        }  
       }  
       if (!this.anfrage) {  
        alert ("Das Anfrageobjekt konnte nicht initialisiert werden.\n\nBitte versuchen Sie es mit einen Anderen Browser.");  
        return false;  
       }  
        
       this.checkState = function() {  
        if (this.readyState==2) {  
         thisObject.onWaiting();  
        } else if (this.readyState==4 && this.status==200) {  
         thisObject.onSuccess();  
        } else if (this.readyState==4 && this.status!=200) {  
         thisObject.onFailure();  
        }  
       }  
        
       this.sendRequest = function() {  
        var parameter=this.parameter;  
        if (this.method=="GET"){  
         this.anfrage.open("GET",this.is_async,this.url + "?" + parameter);  
         this.anfrage.onreadystatechange = this.checkState;  
         this.anfrage.send(null);  
        }  
       };  
      }  
        
      var anfrage = new AjaxRequest("http://localhost/kapitel01/boards/aktuelleBoardVerkaeufe.php",{  
        method:"GET",  
        parameter:"wort=test",  
        is_async: true,  
        onWaiting:test,  
        onSuccess:test,  
        onFailure:test  
       });  
        
      function test() {alert ("test");}  
        
      anfrage.sendRequest();  
      
      

      und nochmals danke für die rasche antwort.

  2. Hi,

    this.checkState = function() {
      if (this.readyState==2) {
       anfrage.onWaiting();

    closure probiert?
    var obj = this;
    this.checkState = function() {
        obj.onWaiting();

    Gruesse, Joachim

    --
    Am Ende wird alles gut.