I'm new to JavaScript and this is annoying me. I have defined a "class"
(JavaScript OO seems really strange to me...) in the following way:
function StateSuggestions(pSource) {
this.source = pSource;
this.req=new ActiveXObject("Msxml2.XMLHTTP"); //this.req != null
this.Process = MyProcess;
this.requestSuggestions = MyRequestSuggestions;
...
//requestSuggestions is called from Outside
function MyRequestSuggestions(pAutoSuggestControl, pTypeAhead) {
this.req.onreadystatechange = this.Process; //this.req !=
null
this.req.open("GET",url, true);
...
function MyProcess (){
this.aSuggestions = [];
if (this.req!=null){ //this.req ==
null......why??????
if (this.req.readyState == 4){
if (this.req.status == 200){
...
All seems to work fine, except that this.req is null when MyProcess is
reached...
When creating the object this.req != null, in MyRequestSuggestions
this.req != null, but when MyProcess is reached, this.req turns to null
Best Regards
Fabio Cavassini
VK - 30 Nov 2005 07:50 GMT
> I'm new to JavaScript and this is annoying me. I have defined a "class"
> (JavaScript OO seems really strange to me...) in the following way:
Well, your definition is rather strange from the point of view of any
OOP language ;-)
If you vant to assign a function result, you use:
this.member = someFunction();
If you vant to assign a new object instance, you use:
this.member = new someFunction();
In the listed example you assign a constructor reference to your member
which is rather pointless except some really special occasions.
Presuming I decrypted your original intentions properly:
function StateSuggestions(pSource) {
this.source = pSource;
this.req=new ActiveXObject("Msxml2.XMLHTTP"); // > IE 5.x only !
this.Process = new MyProcess();
this.requestSuggestions = new MyRequestSuggestions();
// ...
}
<snip>
At this point I seem having lost the thread. You know, irrelevant to
JavaScript specifics you have to take a decision who is who in your
object. Say "Object A has instances of object B ans C as its members";
or: "Object C has instances of object A ans B as its members".
The situation when "Object A has an instance of B as a member and B has
an instance of A as a member" are not in common use unless you writing
a "Hacking OOP" book. Could you post a block-scheme of the desired
structure?
Julian Turner - 30 Nov 2005 07:52 GMT
[snip]
> this.req.onreadystatechange = this.Process; //this.req !=
AFAIK, the problem is here, and your understanding of the "this"
keyword.
Try instead:-
var INSTANCE=this;
this.req.onreadystatechange = function() {INSTANCE.Process();};
In rough terms, the value of "this" within a function depends on what
is calling the function.
If I have:-
myObjInstance.Process()
then "this" is set to "myObjInstance" within the Process function.
However if you just call
Process()
then "this" refers to the window object in the browser. I.e. it is as
if you called:-
window.Process()
In your code, the effect of the assignment expression
this.req.onreadystatechange = this.Process;
Is to pass a reference to the "Process" function alone, which does not
include the "this." context.
Accordingly when "onreadystatechange" is fired, is is effectively
calling "Process()", not "obj.Process()".
So "this" will point to the window object, which does not have a "req"
property.
The solution provided above uses closures.
Read this article on closures:-
<URL:http://jibbering.com/faq/faq_notes/closures.html>
Regards
Julian Turner
Fabio Cavassini - 30 Nov 2005 14:16 GMT
Greeaattt!
Not only it works perfectly, now I understand why, thanks Julian ;)
This code is part of a "Google Suggest" like implementation, let me
know if you are interested in it...
Best Regards