I've created a new class using prototype.js.
That means nothing to anyone not intimately familiar with prototype.js.
After I make the ajax.request all references to
this.myClassMethodorVariable are lost.
No evidence for that is presented here (including any indication of what
"references to this.myClassMethodorVariable" is supposed to mean).
Does the ajax method blow out the object persistance?
What would you regard as "object persistence"?
I'm fairly new to OOP javascript so could be (and probably
am) overlooking some detail.
Or you are hiding from learning what OO javascript is behind someone
else's library.
Below is the logic of what i'm trying to do.
Demonstrations are more effective than incomplete descriptions.
var myAjax = new Ajax.Request( this.WSURL, { method: 'get',
parameters: pars, onSuccess: this.myHandler});
In the same way as - this.WSURL - is passed to the method call as a
string value (it is resolved to a value and that value becomes the
argument), - this.myHandler - is assigned to a property of the object
that is the second arguemnt to the mehtod call as a reference to a
function object. Function objects that are assigned to properties of an
object do not know that they have been assigned as properties of an
object (indeed they could not as they may validly be assigned as
properties of any number of (any type/'class' of) objects, as is the
case here where the same function object is a property of your -
myClass.prototype - object, and is also a (onSuccess) property of the
anonymous object passed to the method call.)
},
myHandler: function(ajaxResponse) {
this.myMessage(ajaxResponse.responseText);
},
<snip>
The value of the - this - keyword in javascript is determined entirely,
and only, by how a function is called (and at the point of calling the
function). If it is called as a method of an object (using a property
accessor) then the - this - value will be a reference to that object. If
it is called in any other way the - this - value will be a reference to
the ECMAScript global object.
The - Ajax.Request - method may call the function referred to by -
this.myHandler - as a method of the anonymous object passed to the
method as an argument, or it may call it having assigned a reference to
it to a local variable (through an Identifier). Thus the - this - value
will either be the anonymous object or the global object, but not the -
myClass.prototype - or any instance of - myClass -, it is impossible to
tell without the code of - Ajax.Request -. Neither of the object that
may be - this - have a 'myMessage' property.
Maintaining associations between javascript custom objects and their
method when using the methods as call-back functions is often done with
closures in javascript:-
<URL:
http://jibbering.com/faq/faq_notes/closures.html >
Richard.