-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.js
121 lines (87 loc) · 1.99 KB
/
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
// 오브젝트는 key value의 집합이다
// object ={key:value}
// 1.Literals and properties
const obj={};
const obj2=new Object();
function print(name,age){
console.log(name);
console.log(age);
}
const ellie={name:'ellie',age:4};
print(ellie);
// can add properties later
ellie.hasJob=true;
console.log(ellie.hasJob);
// can delete properties later
delete ellie.hasJob;
console.log(ellie.hasJob);
// 2.Computed properties
// key should be alway string
// 다시받을때
console.log(ellie.name);
console.log(ellie['name']);
ellie['hasJob']=true;
console.log(ellie.hasJob);
function printValue(obj,key){
console.log(obj[key]);
}
printValue(ellie,'name');
// 3.Property value shorthand
const person1={name:'bob',age:2};
const person2={name:'steve',age:3};
const person3={name:'dave',age:4};
const person4=new Person('elile',30);
console.log(person4);
// function makePerson(name,age){
// return{
// name:name,
// age:age,
// };
// }
// 4.Construct function
function Person(name,age){
this.name=name;
this.age=age;
}
// 5.in operator:property existence check
console.log('name' in ellie);
console.log('age' in ellie);
console.log('random' in ellie);
console.log(ellie.random);
// 6.for..in vs for..of
console.clear();
for(key in ellie){
console.log(key);
}
// for(value of iterable)
const array=[1,2,4,5];
for(let i =0;i<array.length;i++){
console.log(array[i]);
}
for(value of array){
console.log(value);
}
// 7.Fun cloning
const user={name:'ellie',age:'20'};
const user2=user;
user2.name='coder';
console.log(user);
// old way
const user3={};
for(key in user){
user3[key]=user[key];
}
console.clear();
console.log(user3);
// object.assign
const user4={};
Object.assign(user4,user);
console.log(user4);
const user5=Object.assign({},user);
console.log(user5);
// another example
const fruit1={color:'red'};
const fruit2={color:'blue',size:'big'};
const mixed=Object.assign({},fruit1,fruit2);
console.log(mixed.color);
console.log(mixed.size);