04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7: Anonymous Functions<br />

The two highlighted lines show the difference between this example and the previous one. Before<br />

defining the anonymous function, a variable named that is assigned equal to the this object. When the<br />

closure is defined, it has access to that , since it is a uniquely named variable in the containing function.<br />

Even after the function is returned, that is still bound to object , so calling object.getNameFunc()()<br />

returns “ My Object ” .<br />

Both this and arguments behave in this way. If you want access to a containing scope ’ s arguments<br />

object, you ’ ll need to save a reference into another variable that the closure can access.<br />

Memory Leaks<br />

The way closures work causes particular problems in Internet Explorer due to the different garbage -<br />

collection routines used for JScript objects versus COM objects (discussed in Chapter 4 ). Storing a scope<br />

in which an HTML element is stored effectively ensures that the element cannot be destroyed. Consider<br />

the following:<br />

function assignHandler(){<br />

var element = document.getElementById(“someElement”);<br />

element.onclick = function(){<br />

alert(element.id);<br />

};<br />

}<br />

This code creates a closure as an event handler on element , which in turn creates a circular reference<br />

(events are discussed in Chapter 12 ). The anonymous function keeps a reference to the<br />

assignHandler() function ’ s activation object, which prevents the reference count for element from<br />

being decremented. As long as the anonymous function exists, the reference count for element will be at<br />

least 1, which means the memory will never be reclaimed. This situation can be remedied by changing<br />

the code slightly, as shown here:<br />

function assignHandler(){<br />

var element = document.getElementById(“someElement”);<br />

var id = element.id;<br />

element.onclick = function(){<br />

alert(id);<br />

};<br />

}<br />

element = null;<br />

In this version of the code, a copy of element ’ s ID is stored in a variable that is used in the closure,<br />

eliminating the circular reference. That step alone is not enough, however, to prevent the memory<br />

problem. Remember: the closure has a reference to the containing function ’ s entire activation object,<br />

which contains element . Even if the closure doesn ’ t reference element directly, a reference is still stored<br />

in the containing function ’ s activation object. It is necessary, therefore, to set the element variable equal<br />

to null . This dereferences the COM object and decrements its reference count, assuring that the memory<br />

can be reclaimed when appropriate.<br />

190

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!