Javascript object-oriented Object (Object) _ js object-oriented

Source: Internet
Author: User
Object creation declaration in Javascript Object-oriented 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

The 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:

The 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:

The 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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.