-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.object.js
123 lines (98 loc) · 2.46 KB
/
6.object.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* 对对象的扩展 */
let name = 'Strive';
let age = 18;
let json = {
name, //name:name,
age, //age:age
/* showA: function(){
return this.name;
} */
showA() {
return this.name;
},
showB() {
return this.age;
}
};
console.log(json.showA(), json.showB());//Strive 18
//复习下解构
let x = 10;
let y = 20;
function show({x,y}){
console.log(x,y);
}
show({x,y});
/* Object.is() */
// console.log(NaN == NaN);//false 两个对象不相等
// console.log(Number.isNaN(NaN));//true
/* Object.is() 看着长相 一样就相等 否则不相等*/
let b = Object.is(NaN,NaN);
console.log(b);//true
console.log(+0 == -0);//true
console.log(Object.is(+0,-0));//false 看着长相 一样就相等 否则不相等
console.log(Object.is('aaa','aac'));//false
/* 3.Object.assign() 用来合并对象 */
//将三个变量合并
let json = {a:1};
let json2 = {b:2};
let json3 = {c:3};
let obj = Object.assign({},json,json2,json3);
console.log(obj);//{ a: 1, b: 2, c: 3 }
let json = {a:1};
let json2 = {b:2,a:2};//后面传入a:2 会覆盖前面的a:1
let json3 = {c:3};
let obj = Object.assign({},json,json2,json3);
console.log(obj);//{ a: 2, b: 2, c: 3 }
//复制一个对象到另一个
let arr = ['apple', 'banana', 'orange'];
let arr2 = Object.assign([],arr);//将数组合并到新的数组 将数组复制一份到新的里面
arr2.push('tomato');//为数组2 添加变量
console.log(arr2);
console.log(arr);
/* Object.keys() */
let json = {
a:1,
b:2,
c:3
};
for (let key of Object.keys(json)) {
console.log(key);
/* 结果是
a
b
c
*/
}
//使用解构
let {keys,values,entries} = Object;
let json = {
a:1,
b:2,
c:3
};
for (let key of keys(json)) {
console.log(key);
/* 结果是
a
b
c
*/
}
for (let value of values(json)) {
console.log(value);
}
for(let item of entries(json)){
console.log(item);
}
for(let [key,val] of entries(json)){
console.log(key,val);
}
/*对象身上: 对象的扩展运算*/
let {x,y,...z} = {x:1,y:2,a:3,b:4};
console.log(x,y,z);//1 2 { a: 3, b: 4 }
let json = {a:3,b:4};
let json2 = {...json};//拷贝json对象
console.log(json2);//{ a: 3, b: 4 }
delete json2.b;//删除json2的b
console.log(json2);//{ a: 3 }
console.log(json);//{ a: 3, b: 4 }