Object creation instances in JavaScript with annotations _js object-oriented

Source: Internet
Author: User
object creation declaration in javascript:
var obj = {}; or var obj = new Object ();
To add a property to an object, method:
===== the first method of writing ====================================

Obj.name = ' xiaoming '; Add attributes to an object
Obj.updatename = function (name) {//To define UpdateName method for object
THIS.name = name;
}
alert (obj.name);
Obj.updatename ("Xiao Qiang"); Call UpdateName Modify the Name property value of the Obj object
Alert (obj[' name '));
The first time the results are displayed: xiaoming
The second display result is: Xiao Qiang
===== the second way of writing ====================================
obj[' name ' = ' John '; Add attributes to an object
obj[' updatename '] = function (name) {//define UpdateName method for object
obj[' name '] =name;
};
alert (obj.name);
Obj.updatename (' Dick '); Call UpdateName Modify the Name property value of the Obj object
Alert (obj[' name '));
The first time the results are displayed: John
The second display result is: dick
===== the third way of writing ====================================
var obj = {
Name: ' Harry ',//Add property to Object
Updatename:function (name) {///defines UpdateName method for an object
THIS.name = name;
}
};
Alert (obj. name);
Obj.updatename ("Zhao Liu"); Call UpdateName Modify the Name property value of the Obj object
Alert (obj. name);
The first time the results are displayed: Harry
The second display result is: Zhao Liu
===== Analysis =====================================
The first is the most common type of object, because JavaScript is a dynamic language, unlike Java and. NET,
After the program is run and the object is created, you can also modify the internal structure of the object.
For example, adding properties and methods (the reflection mechanism in Java and. NET does not do this).
(a): var obj = {} | | New Object ();
(b): Obj.name = "John";
(c): Obj.updatename = function (name) {this.name = name};

When the program executes (a), it creates an empty object (without any methods and properties) obj,
When the program executes (b), the internal structure of obj is changed, and an attribute name is added.
When the program executes (c), it changes the internal structure of obj, adding a method UpdateName,
And this is all done at the time of the operation

The second way of writing is like an array, but by no means an array, distinguishing whether an array can be so judged:
if (typeof (obj.length) = = "undefined") {
Alert ("Obj is not an array, and the array has a length property!") ");
}else{
Alert ("obj is an array!") ");
}
The second type of writing is more like a data structure: map, such as: obj[key] = value;
Key is a string, value can be any type, variable, object, function, and so on.
You can traverse the internal structure of an object in this way:
for (var key in obj)
{
alert (key);
var value = Obj[key];
alert (value);
}
You can show what you define by alert.
The third way of writing is the internal structure of the map, an object that is fully represented by the Key:value key-value pairs
The JSON object is also this structure, and it's easy to understand the map or the JSON object as long as it's familiar.

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.