1, Object
The concept: objects can {...} and contains several optional attributes to create the curly braces. When a property key-value pair, the key is a string value may be any type.
Creating objects
. 1 the let = User new new Object (); // constructor Syntax 2 the let User = {}; // "literal" grammar
Object attributes:
. 1 the let = {// User object 2 name: "John", // key "name", the value of "John" . 3 Age: 30 // "key" age ", a value of 30 . 4 }; . 5 . 6 attributes key, in front of the colon ":" the values on the right side of the colon
Adding and deleting objects properties
1 user.sex = "man"; // add properties of objects 2 the Delete user.sex; // object deletion
Use square brackets of the object
1 // word processing multiple attribute 2 the let User = {}; . 3 . 4 User [ "like Birds"] = to true; // set . 5 . 6 Alert (User [ "like Birds"]); // use to true . 7 . 8 Delete User [ "like birds"]; // delete
Computed Property
1 // Example a: 2 the let Fruit = "Apple"; . 3 the let = {Bag . 4 [Fruit]:. 5 . 5 }; . 6 Alert (bag.apple); . 7 . 8 // two examples: . 9 the let Fruit = "Apple" ; 10 the let Bag = {}; . 11 Bag [Fruit] =. 5; 12 is // three examples of 13 is the let Fruit = "Apple"; 14 the let Bag = { 15 [Fruit + "Test"]:. 5; 16 }; . 17 Alert (bag.fruittest);
Shorthand property value
When the key and the same attribute values may be written as follows
User = {the let
name, name and where // name: the same name
Age: 13 is
}
Check the existence of attributes like value
Analyzing manner by acquiring attribute is undefined if not present, is used in another way keywords, but there are special circumstances. Specific examples are as follows:
. 1 the let = {User); 2 . 3 Alert (user.isExist == undifined) // to true indicates that the property does not exist . 4 . 5 Alert ( "ISEXIST" in User); // attribute to false absent . 6 . 7 // special circumstances 8 . 9 the let {obj = 10 Test: undefined . 11 } 12 is 13 is Alert (obj.test == undefined) // to false, but this attribute is present, so that in this way not appropriate when using 14 Alert ( "Test" in obj) ; // true indicates that the property exists.
for ... in loop
1 let user = { 2 name: "John", 3 age: 30, 4 isAdmin: true 5 }; 6 7 for(let key in user) { 8 // keys 9 alert( key ); // name, age, isAdmin 10 // 属性键的值 11 alert( user[key] ); // John, 30, true 12 }
Reference copy
. 1 the let = {User name: 'John'}; 2 . 3 the let ADMIN = User; . 4 . 5 admin.name = 'Pete'; // change the reference of "admin" . 6 . 7 Alert (the user.name); // 'Pete ', Changes are at the Seen from "the User" Reference 8 above example shows there is only one object, like our drawers have two keys, if a key (admin) to use the
drawer, use another key later ( user) is open, you will see a change.
Comparison of reference
1 // two references point to the same object when they are equal. 2 the let A = {}; . 3 the let B = A; // copy reference . 4 . 5 Alert (A == B); // to true, both variables refer to the same object . 6 Alert (B === A); // to true 7 8 // If it is two different attributes, they are not equal, even if empty. . 9 the let A = {}; 10 the let B = {}; // two separate objects . 11 12 is Alert (A == B); // to false
Const object
1 // const be a modified object can be modified. 2 const = {user . 3 name: "John" . 4 }; . 5 . 6 user.age = 25; // (*) . 7 . 8 Alert (user.age); // 25 . 9 // user but can not modify the object 10 const = {User . 11 name: "John" 12 is }; 13 is 14 // error (can not assign User assignment) 15 User = { 16 name: "Pete" . 17 };
Object replication and merging Object.assign
Another copy of an object variable is also equivalent to the creation of this object.
1 // manipulation of the original code 2 the let = {User . 3 : "John", name . 4 30: Age . 5 ;} . 6 . 7 the let clone = {}; // new empty object . 8 . 9 // copy all attribute values 10 for (in the let User Key) { . 11 clone [Key] = User [Key]; 12 is } 13 is 14 // now independent replication copy 15 clone.name = "Pete"; // change the value 16 . 17 alert (user.name); // original object property value remains unchanged
To achieve with Object.assign
Syntax: Object .
assign
(
dest [
,
SRC1 ,
src2 ,
SRC3...
]
)
- Parameters
dest
andsrc1, ..., srcN
(can be many) are objects. - This method replicates
src1, ..., srcN
all objects todest
. In other words, beginning from the second parameter, the properties of all objects are copied to the first parameter object, and returnsdest
.1 let user = { 2 name: "John", 3 age: 30 4 }; 5 6 let clone = Object.assign({}, user);
Special circumstances, the object is still the object
. 1 the let {User = 2 name: "John", . 3 sizes: { . 4 height: 182, . 5 width: 50 . 6 } . 7 }; . 8 . 9 the let clone = Object.assign ({}, User); 10 . 11 Alert (User .sizes === clone.sizes); // true, the same object 12 is 13 is // User sizes shared objects and clone 14 user.sizes.width ++; // here to change the value of an attribute 15 Alert (clone.sizes. width); // 51