Application of Objects in ECMAScript

The creation and destruction of objects occur during the execution of JavaScript, understanding the meaning of this paradigm is crucial to understanding the entire language.

Declaration and Instantiation

The way to create an object is to use the keyword 'new' followed by the name of the class to be instantiated:

var oObject = new Object();
var oStringObject = new String();

The first line of code creates an instance of the Object class and stores it in the variable oObject. The second line of code creates an instance of the String class and stores it in the variable oStringObject. If the constructor has no parameters, the parentheses are not required. Therefore, the two lines of code above can be rewritten in the following form:

var oObject = new Object;
var oStringObject = new String;

Object References

In the previous chapters, we introducedConcept of Reference TypesIn ECMAScript, you cannot access the physical representation of an object; you can only access the reference to the object. Each time an object is created, what is stored in the variable is a reference to the object, not the object itself.

Object Dereference

ECMAScript has a garbage collection routine for unused storage units, meaning that it is not necessary to explicitly destroy objects to release memory. When there are no more references to an object, the object is said to be dereferenced. When the garbage collection routine runs, all dereferenced objects are destroyed. The garbage collection routine runs after a function has executed its code, releasing all local variables, and it also runs in some other unpredictable situations.

Setting all references to an object to null can force the removal of the object. For example:

var oObject = new Object;
// do something with the object here
oObject = null;

When the variable oObject is set to null, the reference to the first created object does not exist anymore. This means that the object will be destroyed the next time the garbage collection program runs.

It is a good habit to release an object after using it to free up memory. This also ensures that objects that are no longer accessible are no longer used, thereby preventing errors in program design. In addition, old browsers (such as IE/MAC) do not have a comprehensive garbage collection program, so objects may not be destroyed correctly when the page is unloaded. The best way to ensure correct memory usage is to remove the object and all its properties.

Note:Be careful when removing all references to an object. If an object has two or more references, all references must be set to null correctly to properly remove the object.

Early Binding and Late Binding

Binding refers to the method of combining an object's interface with an object instance.

Early binding refers to defining the properties and methods of an object before instantiating it, which allows the compiler or interpreter to convert machine code in advance. With early binding, such as in languages like Java and Visual Basic, you can use IntelliSense (which provides a list of properties and methods in the object to developers) in the development environment. ECMAScript is not a strongly typed language, so it does not support early binding.

On the other hand, late binding refers to the situation where the compiler or interpreter does not know the type of the object before runtime. With late binding, there is no need to check the type of the object; it is only necessary to check whether the object supports properties and methods. All variables in ECMAScript use late binding, allowing for a large number of object operations without any penalty.