prototype.js object persistance

P

petermichaux

R

Richard Cornford

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.
 
B

bigdadro

Richard,

Thanks for helping me out with my problem. Being the all knowing
javascript "wizard" you are, you could have at least provided me with
some positive guidance instead of what appears to be ridicule. Keep up
the good work, dude!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top