- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
Latest commit
99 lines (82 loc) · 2.16 KB
/
Copy pathobject.js
File metadata and controls
99 lines (82 loc) · 2.16 KB
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
'use strict';
// Object
// one of the JavaScript's data types
// a collection of related data and/or functionality
// Nearly all objects in JavaScript are instances of Object
// object = { key : value };
// 1. Literals and properties
constobj1={};// 'object literal syntax
constobj2=newObject();// 'object constructor' syntax
functionprint(person){
console.log(person.name);
console.log(person.age);
}
constsill={name: 'sill',age: 20};
print(sill);
sill.hasJob=true;
console.log(sill.hasJob);
deletesill.hasJob;
console.log(sill.hasJob);
// 2. Computed properties
// key should be always string
console.log(sill.name);
console.log(sill['name']);
sill['hasJob']=true;
console.log(sill.hasJob);
functionprintValues(obj,key){
// console.log(obj.key); // Error
console.log(obj[key]);
}
printValues(sill,'name');
// 3. Property value shorthand
constperson1={name: 'bob',age: 2};
constperson2={name: 'steve',age: 3};
constperson3={name: 'dave',age: 4};
// const person4 = makePerson('sill', 20);
constperson4=newPerson('sill',20);
console.log(person4);
// 4. Constructor Funciton
functionPerson(name,age){
// return {
// name, // name: name
// age, // age: age
// };
// this = {};
this.name=name;
this.age=age;
// return this;
}
// 5. in operator: Property exsitence check(key in obj)
console.log('name'insill);
console.log('age'insill);
console.log('random',sill);
// console.log(sill.random) // undefined
// 6. for..in vs for..of
// for (key in obj)
console.clear();// 이전 console.log 가 지워짐
for(letkeyinsill){
console.log(key);
}
// for (value of interable)
constarray=[1,2,4,5];
// for(let i = 0; i < array.length; i++) {
// console.log(array[i]);
// }
for(letvalueofarray){
console.log(value);
}
// 7. Fun cloning
// Object.assign(dest, [obj1, obj2, obj3...])
constuser={name: 'sill',age: 20};
constuser2=user;
// user2.name = 'coder';
console.log(user);
// old way
constuser3={};
for(letkeyinuser){
user3[key]=user[key];
}
console.clear();
console.log(user3);
constuser4=Object.assign({},user);
console.log(user4);