3.2Object Application
· Object abolition: if an object has two or more references, you must set all its references to null to abolish the object correctly.
Like java, JS has a garbage collection mechanism that automatically removes objects that are not referenced.
· Early binding and late binding
Binding: a method that combines object interfaces with instances.
Early binding: defines features and methods before instantiating an object;
Play binding: refers to in the compiler or interpretationProgramThe object type is unknown before running. Ecmascript uses late binding.
3.3Object Type
1. Local object
· Array class
The following example is used to describe its usage:
VaR arry = new array (10); // You can omit it if you do not know how big it is.CodeCan be added at will
VaR arry = new array ("red", "green", "blue ");
Alert (arry [1]); // "green"
VaR SCOR = "red, green, blue ";
VaR arry = SCOR. Split (","); // convert to array object
VaR SCOR = "green ";
VaR arry = SCOR. Split (""); // use an empty string to separate the string into the following characters: "g, R, E, E, N"
Array has two methods of string: Concat (): Connection/slice (): intercepting part of content
Array provides the stack function:
VaR stack = new array;
Stack. Push ("red ");
Stack. Push ("green ");
Alert (stack. tostring (); // "Red, Green"
VaR AA = stack. Pop (); // "green"
Shift (): the first entry of the array is deleted and returned as a function value. /Unshift.
Shift and push () can be used to complete the queue function.
· Date class
VaR d = new date (date. parse ("6/1/2011"); // If the passed string cannot be converted to a date, it will be Nan
VaR d = new date (date. UTC (2011,0, 6); // pay special attention to setting the month because the monthly setting starts from 0 (2011-1-6)
2. built-in objects
Ecmascript provides two built-in objects: Global and math;
Global eval () method: This method is like the whole ecmascript interpreter. It accepts a parameter, interprets it as a true ecmascript statement, and inserts it into the position of the function.
3. Host Object
All non-local objects are host objects, that is, objects in the host environment implemented by ecmascript. All BOM and DOM objects are host objects.
3.5Define a class or object
Use the constructor:
Function car (scolor, idoor ){
This. Color = scolor;
This. Doors = idoor;
This. showcolor = function (){
Alert (this. color)
};
}
VaR ocar = new car ('red', 4 );
VaR ocar2 = new car ("green", 3 );
Note: The object structure does not need to be defined and can be used directly in the constructor as needed.
The functions in the above created object are2Memory is wasted. (C ++,JavaThe functions of all objects in)
Improvement: put forward the function. After the constructor, add it with prototype. (The prototype attribute can be used to define the method)
Car. Prototype. showcolor = function () {alert (this. Color };
3.6Modify Object
Create a new method: Number. Prototype. tohexstring = function () {return this. tostring (16 )};
Re-define existing methods: If you re-define them, the existing methods will be overwritten, because ecmascript does not have to be overloaded.
More: http://blog.donews.com/me1105/archive/2011/02/10/118.aspx