I. Type of Object
An object is a collection of properties, a property containing a name (attribute name) and a value (property value).
Object is an ideal choice for storing and transferring data in an application
Second, create an object
There are two ways to create an instance of an object:
- Use the new operator followed by the object constructor
- Use object initializers, which are object literal representations
2.1 use the new operator followed by the object constructor to create an object instance:
1 var New Object (); 2 Person.name = "Nicholas"; 3 person.age = 29;
Like other JavaScript variables, the name of the object (which can be a normal variable) and the name of the property is case-sensitive.
2.2 Create an object instance by using the literal notation of objects:
1 var person = {2 name: "Nicholas",3 age:294 };
Each attribute is separated by "," and the last attribute cannot be added "," otherwise it will be an error in some browsers.
2.2.1 When using literal notation, the property name can also be represented by a string:
1 var person = {2 ' name ': ' Nicholas ',3 ' age ': 29 , 4 5:true5 };
This example contains 3 attributes: Name, age, 5, where the numeric attribute name is automatically converted to a string.
2.2.2 When using object literal notation, if you leave {} blank, you can define an object that contains only the default properties and methods:
1 var person = {}; 2 Person.name = "Nicholas"; 3 person.age = 29;
When passing a large number of optional parameters to a function, it is recommended to use literal notation:
1 functionDisplayInfo (args) {2 varOutput = "";3 4 if(typeofArgs.name = = "string"){5Output + = "Name:" + args.name + "\ n";6 }7 8 if(typeofArgs.age = = "Number") {9Output + = "Age:" + args.age + "\ n";Ten } One A alert (output); - } - the DisplayInfo ({ -Name: "Nicholas", -Age:29 - }); + - DisplayInfo ({ +Name: "Greg" A});
First, use the typeof operator to detect the existence of each property, and then perform an operation on that property.
Three, the object property representation method
3.1-point notation
1 var person = {2 ' name ': ' Nicholas ',3 ' age ':4 }; 5 6 alert (person.name); 7 alert (person.age);
The last two lines are the point notation for the object property.
3.2[] Notation
As follows:
1 var New Object (); 2 Mycar.make = "Ford"; 3 Mycar.model = "Mustang"; 4 mycar.year = 1969; 5 Alert (mycar["make"]+mycar["model"]+mycar["Year"]);
The last line is to use the [] notation to get the object property.
Because each property has a string value to access it (that is, the key in the value pair), the object is sometimes called an associative array.
- properties can be accessed through variables
var pname= "name";
Alert (Cat[pname]);
- If a property name contains a character that causes a syntax error (for example, a property name that has a space or dash, or a number that starts with a digit), or if the property name uses a keyword or a reserved word, you can also use square brackets notation.
cat["lovely brother"]= "Tom"; The property name contains a space so you cannot access it by using dot notation.
- Useful when property names are dynamic (property names can be determined only at run time), as described in the for...in loop below
Iv. enumerating all properties of an object
- For...in cycle
- Object.keys (o)
- Object.getownpropertynames (o)
4.1for...in cycle
This method accesses the properties of an object in turn and all enumerable properties in its prototype chain.
1 var obj = {a:1, b:2, C:3}; 2 for (var in obj) {3 console.log ("obj." + prop + "=" + Obj[prop]); 4 }
Prop is the property name. Obj[prop] is the [] method of getting the property value of an object.
4.2Object.keys (o)
The method returns an array of the names of all the properties that the object itself contains (not included in the prototype), and all the values of the array are strings.
1 var obj={"No1": "A", "No2": "B"}2 alert (Object.keys (obj));
4.3object.getownpropertynames (o)
The method returns an array that contains the o
names of all properties owned by the object, whether enumerable or not.
(not to be continued ...) )
Learn JavaScript from the beginning (11)--object type