Home > Web Front-end > JS Tutorial > Javascript object-oriented Object (Object)_js object-oriented

Javascript object-oriented Object (Object)_js object-oriented

WBOY
Release: 2016-05-16 18:27:28
Original
1356 people have browsed it

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'; // is an object Add attributes
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 result displayed for the first time is: Xiao Ming
The result displayed for the second time is :Xiaoqiang
//======The second way of writing==================================== ==
obj['name'] = 'Zhang San'; //Add attributes to the object
obj['updateName'] = function(name){//Define the updateName method for the object
obj[ 'name'] =name;
};
alert(obj.name);
obj.updateName('李思'); //Call updateName to modify the name attribute value of the obj object
alert (obj['name']);
The first time the result is displayed: Zhang San
The second time the result is displayed: Li Si

Copy code The code is as follows:

//======The 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);

The first time the result is displayed: Wang Wu
The second time the result is: Zhao Liu
//=====Analysis========================================
The first way of writing is the most common way of writing objects, because JavaScript is a dynamic language, different from Java and .Net.
After the program runs and creates the object, the internal structure of the object can also be modified,
For example, 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), the object of obj is changed. The internal structure adds an attribute name.
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 time
The second way of writing is like an array, but it is definitely not an array. To distinguish whether it is an array or not, you can judge it like this:
Copy the code The code is as follows:

if(typeof(obj.length) == "undefined") {
alert("obj is not an array, arrays have the length attribute!");
}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 the object in this way:
Copy code The code is as follows:

for(var key in obj)
{
alert(key);
var value = obj[key];
alert(value);
}

Alert can display the content you define.
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 you are familiar with map or JSON objects It's easy to understand.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template