Urmell
08-14-2008, 08:37 AM
Hello everyone,
I'm new to javascript so I'm trying all kinds of stuff to get a hang of it. I'm used to OOP so that's what I'm trying to do in javascript. It's been going well but now I got stuck at some point. Here's the object I made:
function objTest() {
this.intSomeVar = null;
this.ajaxRequest = null;
this.setTheVar = function(intValue) {
this.intSomeVar = intValue;
}
this.getTheVar = function() {
return this.intSomeVar;
}
this.doubleTheVar = function(intValue) {
var intTemp = this.getTheVar();
intTemp = intTemp * 2;
this.setTheVar(intTemp);
}
this.getVarFromFile = function() {
this.ajaxRequest = GetXmlHttpObject(); //<- some function thats creates the XMLHttpRequest object, it works
this.ajaxRequest.onreadystatechange = function() {
if(this.ajaxRequest.readyState == 4){ //<- not working :(
this.setTheVar(this.ajaxRequest.responseText);
}
}
this.ajaxRequest.open("GET", "somevar.txt", true);
this.ajaxRequest.send(null);
}
}
The function in bold is the one that doesn't work right. Now, I guess the problem is that I'm "creating" that callback function in there, so it's not part of the object, therefore I cant use any methods or properties in the object. How should I go about doing this?
Thanks in advance!
I'm new to javascript so I'm trying all kinds of stuff to get a hang of it. I'm used to OOP so that's what I'm trying to do in javascript. It's been going well but now I got stuck at some point. Here's the object I made:
function objTest() {
this.intSomeVar = null;
this.ajaxRequest = null;
this.setTheVar = function(intValue) {
this.intSomeVar = intValue;
}
this.getTheVar = function() {
return this.intSomeVar;
}
this.doubleTheVar = function(intValue) {
var intTemp = this.getTheVar();
intTemp = intTemp * 2;
this.setTheVar(intTemp);
}
this.getVarFromFile = function() {
this.ajaxRequest = GetXmlHttpObject(); //<- some function thats creates the XMLHttpRequest object, it works
this.ajaxRequest.onreadystatechange = function() {
if(this.ajaxRequest.readyState == 4){ //<- not working :(
this.setTheVar(this.ajaxRequest.responseText);
}
}
this.ajaxRequest.open("GET", "somevar.txt", true);
this.ajaxRequest.send(null);
}
}
The function in bold is the one that doesn't work right. Now, I guess the problem is that I'm "creating" that callback function in there, so it's not part of the object, therefore I cant use any methods or properties in the object. How should I go about doing this?
Thanks in advance!