[Javascript] AJAX app causing excessive browser memory usage

Jonathan Buchanan jonathan.buchanan at gmail.com
Sat May 20 03:17:50 CDT 2006


Rick Emery wrote:
>>> When the response function checks readyState, I get a javascript error
>>> "this.xhr has no properties". I thought I needed to set xhr to
>>> something either in the constructor or by adding it to the
>>> xhrObj.prototype, but what would I set it to? Setting it to the
>>> xmlhttprequest object defeats the purpose of creating and destroying
>>> it on the fly.
>>>
>>> I'm sure I'm just missing something simple...can anybody help me out?
>>>
>>> Thanks in advance,
>>> Rick
>>
>> The problem in this case is that when handleXHRResponse() is called,
>> "this" no longer refers to your xhrObj instance. You can use the
>> built-in call[1] or apply[2] functions to execute the function in the
>> scope of your xhrObj instance.
> 
> The short question: Why doesn't "this" in the handleXHRResponse() point 
> to the object in which it resides (instance of xhrObj)?

Here's a topic which should help answer this and your other questions:

http://www.sitepoint.com/forums/showthread.php?t=364932

Key points:

"...in Javascript this refers to the calling context - not as you would 
expect from other languages, to the owner of the property. In Javacript, 
functions are variables like anything else so they can be passed around 
and referenced from multiple places."

"...functions are not bound to objects. Objects are merely collections 
of functions. When you pass a function as a parameter ... you only pass 
the function - not the owner-object. Thus when the function is called, 
the context will not be your ownerobject."

Jonathan.



More information about the Javascript mailing list