SoFunction
Updated on 2025-04-13

Object creation in javascript with comments

Object creation declaration in javascript:
var obj = {}; or var obj = new Object();
Add attributes to the object, method:
//===== The first way to write===================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

= 'Xiao Ming'; //Add attributes to the object
= function(name){//Define updateName method for the object
= name;
}
alert();
("Xiaoqiang"); //Call updateName to modify the name attribute value of the obj object
alert(obj['name']);
The first time the result is: Xiao Ming
The second time the result is: Xiaoqiang
//===== The second way to write===================================================================================================================
obj['name'] = 'Zhang San'; //Add attributes to the object
obj['updateName'] = function(name){//Define updateName method for the object
obj['name'] =name;
}; 
alert();
('Li Si'); //Call updateName to modify the name attribute value of the obj object
alert(obj['name']);
The first time the result is: Zhang San
The second time the result is: Li Si
//===== The third way of writing===================================================================================================================
var obj = {
name: 'Wang Wu', //Add attributes to the object
updateName: function(name){//Define updateName method for the object
= name;
}
};
alert(obj .name);
("Zhao Liu"); //Call updateName to modify the name attribute value of the obj object
alert(obj .name);
The first time the result is: Wang Wu
The second time the result is: Zhao Liu
//=============================================================================================================================================================================================================================================================
The first way is to write objects, because javascript is a dynamic language, unlike Java and .Net.
After the program runs and creates an 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): = "Zhang San";
(c): = function(name){ = name};

After the program executes (a), an empty object (without any methods and properties) is created obj.
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 these are all actions completed during operation

The second way of writing is like an array, but it is definitely not an array. You can judge this way whether an array is a judgment:
if(typeof() == "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;
A key is a string, and a value can be any type, variable, object, function, etc.
You can traverse the internal structure of the 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 to see the internal structure of map. An object is completely represented by the key: value key-value pair.
JSON objects have this structure, and it is easy to understand as long as you are familiar with map or JSON objects.