-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo09_object_literals.js
145 lines (115 loc) · 3.18 KB
/
Demo09_object_literals.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* * Define Object (JSON) with simple method
*/
console.clear();
//? Shorthand assignment
let a = 3;
let a_object = {a};
console.log(a_object);
let b = 4;
let b_object = {b : b};
console.log(b_object);
//? Method definition
let obj = {
value : 5,
double() {
return this.value * 2;
},
square : function() {
return this.value * this.value;
},
triple : () => {
return obj.value * 3; // Cannot call this.value
}
};
console.log(obj);
console.log("Value of object: ", obj.value);
console.log("Value of object called by method: ", obj.double());
console.log("Value of object called by Square method: ", obj.square());
console.log("Value of object called by Triple method: ", obj.triple());
//? Computed properties
let value = 100;
let pro = {
['product' + value] : 'Number of Product is ' + value,
['method_' + value](x) {
return value + x;
}
};
console.log(pro);
console.log(pro.method_100(50));
//? Symbol ~ is immutable and unique
let s1 = Symbol('Symbol 01');
let s2 = Symbol('Symbol 02');
console.log('s1 == s2', s1 == s2);
console.log('s1 === s2', s1 === s2);
let o = {
[Symbol('id')] : 5,
data : 100
};
console.log(o);
console.log(Object.getOwnPropertyNames(o));
let str = JSON.stringify(o); // convert object to String
console.log("String:", str);
let jsonObject = JSON.parse(str); // convert string to object json
console.log("Object JSON:", jsonObject);
console.log(Object.getOwnPropertyNames(o));
//? Object destructuring
var user = {
id : 1,
name : "Mary",
city : "Tokyo",
role : "Admin"
};
console.log("Object user: ", user);
var {id, name, city, role} = user;
console.log("id:", id);
console.log("name:", name);
console.log("city:", city);
console.log("role:", role);
//? Lock OBJECT
console.log("--- LOCK OBJECT ---");
let pObject = {
id: 1,
name: "Product",
location: {
long: 1.25,
lat: 2.45
},
description: "Note something"
};
console.log("Product is", pObject);
//! add new COLOR property for object
pObject.color = Math.round(Math.random() * 100);
console.log("Product is added color Property", pObject);
//! update value of property
pObject.name = "Person";
console.log("Product name is update", pObject);
//! remove property of object
delete pObject.description;
console.log("Description property is deleted", pObject);
//! SEAL is lock object, cannot add or delete property of Object
console.log(pObject);
Object.seal(pObject);
console.log("After Object.seal(), cannot add or delete property");
pObject.size = [200, 300];
console.log(pObject);
delete pObject.color;
console.log(pObject);
console.log("Object can be change value");
pObject.id = 2;
pObject.location.height = 1000;
console.log(pObject);
//! FREEZE is lock all value and property of object
Object.freeze(pObject);
console.log("After Object.freeze(), cannot change value of object");
pObject.id = 1;
console.log(pObject);
pObject.size = [200, 300];
console.log(pObject);
delete pObject.color;
console.log(pObject);
console.log("But object can changed value of sub-object");
delete pObject.location.height;
console.log(pObject);
pObject.location.long = 99.65;
console.log(pObject);