Home >Web Front-end >JS Tutorial >Object creation in javascript examples with comments_js object-oriented

Object creation in javascript examples with comments_js object-oriented

WBOY
WBOYOriginal
2016-05-16 18:10:531082browse
Object creation statement in JavaScript:
var obj = {}; or var obj = new Object();
Add attributes to the object, method:
//==== =First way of writing====================================

obj.name = 'Xiao Ming'; //Add attributes to the object
obj.updateName = function(name){//Define 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 displayed result is: Xiao Ming
The second display result is: Xiaoqiang
//======The second way of writing========================== ===========
obj['name'] = 'Zhang San'; //Add attributes to the object
obj['updateName'] = function(name){//For Object definition updateName method
obj['name'] =name;
};
alert(obj.name);
obj.updateName('李思'); //Call updateName to modify obj The name attribute value of the object
alert(obj['name']);
The first display result is: Zhang San
The second display result is: Li Si
//=== ==Third way of writing====================================
var obj = {
name: '王五', //Add attributes to the object
updateName: function(name){//Define the updateName method for the object
this.name = name;   
}
};
alert(obj .name);
obj.updateName("Zhao Liu"); //Call updateName to modify the name attribute value of the obj object
alert(obj .name);
First time The displayed result is: Wang Wu
The second displayed result is: Zhao Liu
//======Analysis====================== ================
The first way of writing is the most common way of writing objects, because javascript is a dynamic language, unlike Java and .Net.
When the program is running and After creating an object, you can also modify the internal structure of the object,
such as adding properties and methods (the reflection mechanism in Java and .net cannot do this).
(a): var obj = {} || new Object();
(b): obj.name = "Zhang San";
(c): obj.updateName = function(name) { this.name = name};

When the program executes (a), an empty object (does not contain any methods and properties) obj is created.
When the program executes (b), it changes The internal structure of obj is changed, and an attribute name is added.
When the program executes (c), the internal structure of obj is changed and a method updateName is added.
These are all actions completed during running

The second way of writing is like an array, but it is definitely not an array. To distinguish whether it is an array, you can judge it like this:
if(typeof(obj.length) == "undefined") {
alert(" obj is not an array, arrays have length attributes! ");
}else{
alert("obj is an array!");
}
The second way 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, etc.
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);
}
The content you define can be displayed through alert.
The third way of writing is the internal structure of map at first glance. An object is completely represented internally by key:value pairs.
JSON objects also have this structure. As long as the map or JSON object has What is familiar is easy to understand.
Statement:
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