Continue with the previous Syntax: This article introduces JS custom objects.
In js, you can use functions to simulate object descriptions. You can use several types of definition classes to describe objects:
Method 1: Define an empty function. Use this function to describe a new object and add attributes and methods to the object reference:
[Javascript]
// Use js to describe people
Function Person () {// is equivalent to the constructor.
// Alert ("person run ");
}
// Create an object using the description. New.
Var p = new Person ();
// Dynamically add properties to the p object. Directly use the p. property name.
P. name = "zhangsan"; // After the value is assigned, you can directly use the p. attribute name to read the attribute value.
P. age = 29;
// If the property of the defined p object is assigned to a function, a method is added to the p object.
P. show = function (){
Alert ("show:" + this. name + ":" + this. age );
}
P. show (); // call the method of the custom object
Method 2: Describe the class through the constructor, and define the attributes and methods of the object in the function body. As follows:
[Javascript]
Function Person (name, age ){
// Two attributes are added to the Person object.
This. name = name;
This. age = age;
// Add two methods to the Person object, which indirectly indicate that functions in js can be nested functions.
This. setName = function (name ){
This. name = name;
}
This. getName = function (){
Return this. name;
}
}
Using the Person object above is simple, similar to java:
[Javascript]
Var p = new Person ("James", 20 );
For (x in p ){
Println (x + ": element value:" + p [x]); // in for in, x is a member of the p object. For example, if x is name/age/getName...
}
P. setName ("Xiaoqiang ");
Println ("look at the name Ha:" + p ["name"]) // The object ["attribute name"] can also view the attribute value
Println ("the name can be viewed through the method:" + p. getName ());
Println ("directly use attributes to see the name" + p. name );
Method 3: The attribute name and method of the object are enclosed in a braces {} in the form of a key-value pair. The object constructed in this way cannot create a new object, it is equivalent to the var object variable name. The variable name of this object cannot be new. As follows:
[Javascript]
// Directly use {} to define the key-value pairs of attributes and values. Key-value keys are connected by commas.
Var pp = {
// Define some members.
"Name": "Xiao Ming", "age": 28,
"GetName": function (){
Return this. name;
}
}
Println ("can you check the value?:" + pp ["age"] + ":" + pp. name + ":" + pp. getName ());
This method is more widely used in data. It is used as a storage container object using key-value storage, and is as follows:
[Javascript]
Var citys = {
"Jiangsu": ["Nanjing", "Suzhou", "Wuxi", "Changzhou"],
"Shanghai": ["Xuhui District", "Pudong district", "Baoshan District"]
}
Println ("the city can also play like this:" + citys. Jiangsu [1]); // if you are happy, you can get Jiangsu and give Jiangsu a Traversal
For (var I = 0; I <citys. Jiangsu. length; I ++ ){
Println ("Jiangsu:" + citys. Jiangsu [I]);
}
Conclusion: In general,
1. If the data is in the form of key-value pairs for data storage, the method 3 won't work.
2. to store data without a key-value pair, use a normal array.
3. If you want to play with objects in java, you can use method 2, which is intuitive.