Object creation declaration in javascript:
Var obj = {}; or var obj = new Object ();
Add attributes to an object. Method:
// ======= First writing method ======================================== ======
Obj. name = 'xiaoming '; // adds attributes to the object
Obj. updateName = function (name) {// defines the updateName method for the object
This. name = name;
}
Alert (obj. name );
Obj. updateName ("Xiaoqiang"); // call updateName to modify the name attribute value of the obj object
Alert (obj ['name']);
The first display result is: James
The second result is: Xiaoqiang
// ===== Second Writing Method ========================================== ======
Obj ['name'] = 'zhang san'; // adds an attribute to an object.
Obj ['updatename '] = function (name) {// define the updateName method for the object
Obj ['name'] = name;
};
Alert (obj. name );
Obj. updateName ('Li si'); // call updateName to modify the name attribute value of the obj object
Alert (obj ['name']);
The first display result is: Zhang San
The second result is: Li Si
Copy codeThe Code is as follows:
// ===== Third Writing Method ============================================ ======
Var obj = {
Name: 'wang 5', // adds attributes to the object
UpdateName: function (name) {// updateName method defined for the object
This. name = name;
}
};
Alert (obj. name );
Obj. updateName ("Zhao six"); // call updateName to modify the name attribute value of the obj object
Alert (obj. name );
The first display result is: Wang Wu
The second result is: Zhao Liu
// ======================================================== =====
The first method is the most common method for writing objects, because javascript is a dynamic language, which is different from Java and. Net,
After the program runs and creates an object, you can modify the internal structure of the object,
For example, add attributes and methods (reflection mechanisms in java and. net cannot do this ).
(A): var obj ={}| | new Object ();
(B): obj. name = "James ";
(C): obj. updateName = function (name) {this. name = name };
After the program executes (a), an empty object (excluding any methods and attributes) obj is created,
After the program executes (B), the internal structure of obj is changed, and an attribute name is added,
After the program executes (c), the internal structure of obj is changed, and a method updateName is added,
And this is all the actions completed during running.
The second method is like an array, but it is never an array. You can judge whether an array is an array:
Copy codeThe Code is as follows:
If (typeof (obj. length) = "undefined "){
Alert ("obj is not an array, and the array has the length attribute! ");
} Else {
Alert ("obj is an array! ");
}
The second method is more like a Data Structure: map, for example, obj [key] = value;
Key is a string, and value can be any type, variable, object, function, etc.
You can traverse the internal structure of an object in this way:
Copy codeThe Code is as follows:
For (var key in obj)
{
Alert (key );
Var value = obj [key];
Alert (value );
}
Alert can be used to display the content you have defined.
The third method is to look at the internal structure of the map. An object is expressed completely in the key: value key-value Pair mode.
JSON objects are also of this structure. It is easy to understand as long as you are familiar with map or JSON objects.