Working with Objects - Java Script

Objects are created and destroyed throughout the execution of JavaScript code, and understanding the implications of this paradigm is vital to your understanding of the language as a whole.

Declaration and instantiation

Objects are created by using the new keyword followed by the name of the class you wish to instantiate:

The first line creates a new instance of Object and stores it in the variable oObject; the second line creates a new instance of String and stores it in the variable oStringObject.The parentheses aren’t required when the constructor doesn’t require arguments, so these two lines could be rewritten as follows:

Object references

In ECMAScript,it is not possible to access the physical representation of the object; it is only possible to access references to the object.Every time you create an object, a reference to the object is stored in the variable, not the actual object itself.

Dereferencing objects

ECMAScript has a garbage collection 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 be dereferenced.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 not-so-predictable times.

It is possible to forcibly dereference objects by setting all its references equal to null. For example:

When the variable oObject is set to null, 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 memory. 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 binding describes the method whereby an object’s interface is bound to an object instance.

Early binding 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 languages 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.

Late 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.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Java Script Topics