Home > Web Front-end > JS Tutorial > body text

JavaScript Advanced Programming_Basic Knowledge

WBOY
Release: 2016-05-16 19:22:02
Original
958 people have browsed it
The previous chapter briefly talked about the use of objects, now we will introduce them in detail. Objects are created or destroyed during JavaScript execution, and understanding the implications of this paradigm is critical to understanding the language as a whole.
3.2.1 Declaration and instantiation
Objects are created using the keyword new followed by the name of the class to be instantiated:
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, so the above two lines of code can be rewritten in the following form:
3.2.2 Object Reference
In Chapter 2, the concept of reference types was introduced. In ECMAScript, you cannot access the physical representation of an object, only its reference. Every time an object is created, what is stored in the variable is a reference to the object, not the object itself.
3.2.3 Object Destruction
ECMAScript has a garbage collection program, which means there is no need to specifically destroy objects to release memory. When there are no more references to an object, the object is said to be dereferenced. When you run the garbage collection program, all deprecated objects are destroyed. Whenever a function finishes executing its code, the garbage collector runs, releasing all local variables, and under some other unpredictable circumstances.
Set all references to the object to null to forcefully destroy the object. For example:
When the variable oObject is set to null, the reference to the first created object no longer exists. This means that the object will be destroyed the next time the garbage collector is run.
After each use of an object, it is a good habit to destroy it to release memory. This also ensures that objects that are no longer accessible are no longer used, thus preventing programming errors. In addition, older browsers (such as IE/Mac) do not have comprehensive garbage collection procedures, so objects may not be destroyed correctly when the page is unloaded. Destroying an object and all its attributes is the best way to ensure correct memory usage.
Be careful when destroying all references to an object. If an object has two or more references, to correctly destroy the object, all of its references must be set to null.
3.2.4 Early binding and late binding
The so-called binding is a method that combines the interface of an object with the object instance.
Early binding refers to defining the properties and methods of an object before instantiating it, so that the compiler or interpreter can convert machine code in advance. In languages ​​like Java and Visual Basic, early binding makes it possible to use IntelliSense (the ability to provide developers with a list of properties and methods on their objects) in the development environment. ECMAScript is not a strongly typed language, so it does not support early binding.
On the other hand, late binding means that the compiler or interpreter does not know the type of the object before running it. With late binding, there is no need to check the type of the object, only whether the object supports attributes and methods. All variables in ECMAScript use late binding, which allows a large number of object operations to be performed without penalty.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template