-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.js
87 lines (73 loc) · 2.01 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
// Objects
// 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
const obj1 = {}; // 'object literal' syntax
const obj2 = new Object(); // 'object constructor' syntax
function print(person) {
console.log(person.name);
console.log(person.age);
}
const och = { name: "och", age: 27 };
print(och);
// with JavaScript magic (dynamically typed language)
// can add properties later
// can delete properties later
och.hasJob = true;
console.log(och.hasJob);
// 2. Computed(계산된) properties
// key should be always string
console.log(och.name); // 값을 정확하게 알때
console.log(och["name"]); // runtime에서 결정될때
// 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("och", 27);
console.log(person4);
// 4. Constructor function
function Person(name, age) {
// this = {};
this.name = name;
this.age = age;
// return this;
}
// 5. in operator : property existence check ( key in obj )
console.log("name" in och);
console.log("age" in och);
console.log("random" in och);
console.log(och.random);
// 6. for.. in vs for.. of
// for (key in obj)
console.clear();
for (key in och) {
console.log(key);
}
// for (value of iterable)
const array = [1, 2, 4, 5];
for (value of array) {
console.log(value);
}
// 7. Fun cloning
// Object.assign(dest, [obj1, obj2, obj3...])
const user = { name: "och", age: 27 };
const user2 = user;
console.log(user);
// old way
const user3 = {};
for (key in user) {
user3[key] = user[key];
}
console.clear();
console.log(user3);
// another way
const user4 = Object.assign({}, user);
console.log(user4);
// 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);