Java Script Object
1. How to create
1) Create in literal form
Example: var = stt{x:1,y:2,y:3};
or var = stt{x:1,
Y:2,
' For ': 3
Note Keywords must be placed in the middle of quotation marks
2) Create an object from new
Example: var = new STT ();
Stt.name = ' small fish ';
Stt.age = 20;
3) Creating an object from a constructor function
Example: Function test (name,age,sex) {
THIS.name = name;
This.age = age;
This.sex = sex; }
var STT = Test (Stt,20,nan);
4) through
2. Ways to Get Properties
1) Object name. Property name Acquisition
Example: Alert (stt.name);
2) object name [' property name '] gets
Example: Alert (stt[' name ');
Note that when there is no quotation mark within [], it is not confined to the object itself but the entire program finds the property name.
3. Adding attributes
1) Object name. property name = value;
Example Stt.add = ' Taiyuan ';
4. Delete Attributes
1) Delete Object name. property name;
Example Delete stt.name;
5. Traversing properties
1) for (var i in STT) {
Console.log (i)}
Note: This traversal knowledge outputs the name of the property in the object.
2) for (var i in STT) {
Console.log (Stt[i])}
Note: This traversal can only be used with [] to find the value of the property. Since the traversal of the resulting property name is string with quotation marks the result is STT. ' The name ' syntax is incorrect.
6. Can write method in object
Example: var = person{
Name = ' Set ';
Age = 20
sex = ' male ';
function speak () {
Console.log (' Speak ');}
}
Person.speak (); Calling methods in an object
Java Script Object