-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods_object.js
More file actions
204 lines (176 loc) · 6.32 KB
/
methods_object.js
File metadata and controls
204 lines (176 loc) · 6.32 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict";
const person1 = {
firstName: "AAA",
age: 30,
}
const person2 = {
firstName: "BBB",
lastName: "CCC"
}
// Object.assign:
Object.assign(person1, person2); // person1 will get/override everything from person2
console.log(person1);
console.log(person2);
// Object.entries:
for (let [keys, values] of Object.entries(person1)) {
console.log(keys, values);
}
// converting into map:
const myMap = new Map(Object.entries(person1));
console.log(myMap);
// Object.vlaues:
// get values only.
let person1Values = Object.values(person1);
console.log(person1Values);
// Object.kyes:
// get keys only.
let person1Keys = Object.keys(person1);
console.log(person1Keys);
// using for...in loop:
for (let x in person1) {
console.log(person1[x]);
}
// Object.defineProperty:
Object.defineProperty(person1, "address", { value: "BD" }); // addding new property
for (let x in person1) {
console.log(x);
}
console.log(person1);
/**
* in the above loop, we can see that there are only 3 properties. but when we print the object, we can see that there are 4 properties.
*
* when we add properties in normal way. we can edit values, we can see them using loops or other iteration methods. but when we use Object.defineProperty method to add a new property, there are some extra details come up with: writable, enumerable and configurable. by default these are all false.
*
* if enumerable is false, we cannot iterate it. but we can see it in console log.
* if writable is false, we cannot update the value.
* if configurable is false, property cannot be deleted, changed, changed property type, cannot set to true ever again.
*
* if configurable is true, we can modify or delete the property freely. also we can change writable and enumerable from true to false or false to true.
*
* if configurable is false, we cannot change enumerable, but we can change writable from true to false and cannot change it false to true again. if writable is alreay false, we cannot set it to true. we cannot even modify or delete the property.
*/
// person1.address = "USA"; // for use strict: TypeError: Cannot assign to read only property 'address' of object
try {
person1.address = "USA";
console.log("successfully modify address");
} catch (e) {
console.log("failed to modify address");
}
console.log(person1); // cannot change address value
Object.defineProperty(person1, "occupation", { value: "Engineer", configurable: true, writable: true });
console.log(person1);
person1.occupation = "hello";
console.log(person1); // occupation value is changed
for (let x in person1) {
console.log(x);
// cannot see occupation and address, because enumerable is false by default.
}
Object.defineProperty(person1, "hobby", { value: "Gardening", configurable: true, enumerable: true });
for (let x in person1) {
console.log(x);
// after setting enumerable to true, we can see new added property now.
}
try {
Object.defineProperty(person1, "address", { configurable: true }); // TypeError: Cannot redefine property: address
console.log(person1);
} catch (e) {
console.log(e);
}
try {
Object.defineProperty(person1, "address", { writable: true }); // TypeError: Cannot redefine property: address
console.log(person1);
} catch (e) {
console.log(e);
}
Object.defineProperty(person1, "birthPlace", { value: "BD", writable: true, enumerable: true });
console.log(person1);
try {
Object.defineProperty(person1, "birthPlace", { writable: false });
console.log(person1);
} catch (e) {
console.log(e);
}
try {
Object.defineProperty(person1, "birthPlace", { writable: true }); // Cannot redefine property: BirthPlace
console.log(person1);
} catch (e) {
console.log(e);
}
try {
Object.defineProperty(person1, "birthPlace", { enumerable: false }); // Cannot redefine property: BirthPlace
console.log(person1);
} catch (e) {
console.log(e);
}
// Object.getOwnPropertyNames:
console.log(Object.getOwnPropertyNames(person1)); // returns property names as array. this method also returns properties that is not enumerable.
// Object.keys:
console.log(Object.keys(person1)); // only returns enumerable object properties.
// getter:
// Object.defineProperty(person1, "age", {get: function(){ // connot put same name as property
Object.defineProperty(person1, "getAge", {
get: function () {
console.log("Age of", this.firstName, "is", this.age);
}
});
person1.getAge;
// we can add getAge function as property in a simple way. add as a getter provide simpler sytax. no need to use getAge() -> this parenthesis
// setter:
Object.defineProperty(person1, "setName", {
set: function (name) {
this.firstName = name;
}
// set: (name) => {
// person1.firstName = name;
// }
});
Object.defineProperty(person1, "getName", {
get: function () {
console.log("Name:", this.firstName);
}
// get: () => {
// console.log("Name:", person1.firstName);
// }
});
person1.setName = "Boss";
person1.getName;
// Object.preventExtensions: prevent adding new properties
person1.language = "Fr"; // can add new properties
console.log(person1);
Object.preventExtensions(person1);
// try to add new property:
try {
person1.cost = "$500"; // for non-strict mode nothing will happen
console.log("successfully addede new property");
} catch (e) {
console.log("failed to add new property");
}
console.log(person1);
// Object.isExtensible: return true/false
console.log(Object.isExtensible(person1));
// Object.seal: prevent adding and deleting new properties
delete person1.hobby;
console.log(person1);
Object.seal(person1);
try {
delete person1.address;
console.log("successfully deleted address after seal");
} catch (e) {
console.log("failed to deleted address after seal");
}
// Object.isSealed: return true/false
console.log(Object.isSealed(person1));
// Object.freeze: prevent any changes
person1.firstName = "Big boss"; // still able to modify
console.log(person1);
Object.freeze(person1);
try {
person1.firstName = "Last boss";
console.log("successfully modify firstName");
} catch (e) {
console.log("failed to modify firstName");
}
console.log(person1);
// Object.isFrozen: return true/false
console.log(Object.isFrozen(person1));
// With const you can not re-assign the object, but you can still change the value of a property, delete a property or create a new property.