The Partial Function Application in JavaScript / 3 | WebReference

The Partial Function Application in JavaScript / 3


[prev]

The Partial Function Application in JavaScript [con't]

The testBindWithGettersFunction() function displays the object name, arguments and the getter methods in an alert box. The getters are called from a separate function, called testGetters(), so we can validate that the getters are indeed public. Another way to test the validity of this assumption is that you can't call them from within testBindWithGettersFunction() without adding the full function identifier ahead of the getter - e.g.: testBindWithGettersFunction.getTest():

Here's our nicely formatted alert box showing that everything was set as expected: the this pointer refers to our object; the arguments have all been passed to the bound function; the getters have been created: (See Figure 2)

Beware of Circular References

It's necessary to be careful about avoiding circular references when referencing document objects from within an inner function. A common source of circular references is registering an event handler on an element, such that the object has a reference back to the element. Normally the garbage collection would destroy the objects once there are no remaining references to it, but in the event of a circular reference, each object holds a reference to each other, so neither can be flagged for garbage collection. Internet Explorer and Mozilla Firefox are the two Web browsers most commonly associated with memory leaks in JavaScript because they both use a "reference-counting" garbage collection system for memory allocation and retrieval. In the following example, a JavaScript object (obj) contains a reference to a DOM object (referenced by the id "testElt"). The DOM element, in turn, has a reference to the JavaScript obj. The resulting circular reference between the JavaScript object and the DOM object causes a memory leak:

Knowing what to look for is half the battle. From there, all that's left is to remove the circular reference from the equation. Here are a couple of methods:

Set the object to null after you're done with it.

This solution goes back to the old days of programming when developers had to deallocate memory themselves. It works because it takes two objects to tango:

The following fix, advocated on the Microsoft Developer Network, is to simply remove the inner function from the outer one. Doing so effectively eliminates the closure which holds the circular reference in memory:

Now that you know how to avoid circular references, don't be afraid to exploit this tremendously powerful feature of the JavaScript language. Having the ability to pre-populate function arguments and create private variables on the fly is definitely something that developers should take advantage of. In fact, it's the partial application of functions, along with the prototype property that have paved the way for powerful Ajax and JavaScript frameworks such as Prototype.js and script.aculo.us. You might like to have a look at the prototype.org Web site because that's what we'll be covering next time.

References

Original: September 2, 2008


[prev]