In Chapter 1, the concept of reference types was introduced. In ECMAScript, it is not possible to access
the physical representation of the object; it is only possible to access
to the object. Every time
you create an object, a reference to the object is stored in the variable, not the actual object itself.
ECMAScript has a
routine, meaning that you don’t have to specifically destroy objects
in order to free up the memory. When there are no remaining references to an object, the object is said to
. When the garbage collector is run, all dereferenced objects are destroyed. The garbage
collector runs whenever a function has completed its code, freeing up all local variables, and at other
It is possible to forcibly dereference objects by setting all its references equal to
. For example:
var oObject = new Object;
//do something with the object here
oObject = null;
When the variable
is set to
, there are no longer any references to the object created in the
first line. This means that the next time the garbage collector is run, this object will be destroyed.
It’s always a good idea to dereference an object as soon as you’re done using it in order to free up mem-
ory. Doing so can also prevent programming errors by ensuring that you aren’t using an object that
should no longer be accessible. Additionally, older browsers (such as IE/Mac) don’t have conscientious
garbage collectors, so objects may not be properly destroyed when a page is unloaded. Dereferencing an
object and all its properties is the best way to ensure proper memory usage.
Early versus late binding
The concept of
describes the method whereby an object’s interface is bound to an object instance.
means that properties and methods are defined for an object (via its class) before it is
instantiated so the compiler/interpreter can properly assemble the machine code ahead of time. In lan-
guages such as Java and Visual Basic, early binding allows for the use of IntelliSense (the capability that
gives the developer a list of available properties and methods for a particular object) in development
environments. ECMAScript isn’t strongly typed, so it does not support early binding.
, on the other hand, means that the compiler/interpreter doesn’t know what type of object is
being held in a particular variable until runtime. With late binding, no check is made to determine the
particular type of object, only whether the object supports the property or method. ECMAScript uses late
binding for all variables, which allows a large amount of object manipulation to occur without penalty.
Be careful to dereference all references to an object. If you have two or more refer-
ences to the same object, all of them must be set to
in order to for you properly
dereference the object.
06_579088 ch03.qxd 3/28/05 11:36 AM Page 69