-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.js
350 lines (236 loc) · 9.68 KB
/
objects.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
*********************************** OBJECTS *************************************
In JavaScript, an object is an unordered collection of key - value pairs.Each key - value pair is called a property.
The key of a property can be a string.And the value of a property can be any value, e.g., a string, a number, an array, and even a function.
JavaScript provides you with many ways to create an object.The most commonly used one is to use the object literal notation.
let person = {
firstName: 'John',
lastName: 'Doe',
'building number': 1234
};
console.log(person.firstName); //accessing property
to access building number you need array like notation
console.log(person['building number']); //array like notation
modify values in object
person.lastName = 'ibrahim';
console.log(person);
add new property
person.age = 25;
console.log('new added value:', person.age);
deleting property
delete person.age;
checking if property exist
console.log('lastName' in person);
accessing all key and value using for in loop
for (let key in person) {
console.log(key + ":" + person[key]);
}
console.log(person);
******************* object using new keyword *********************
The regular {... } syntax allows us to create one object.But often we need to create many similar objects, like multiple users or menu items and so on.
That can be done using constructor functions and the "new" operator.
new keyword
var myDetails = new myDetails();
myDetails.age = 25;
myDetails.firstName = 'aamir';
myDetails.lastName = 'ali';
myDetails.greet = function () {
console.log("welllcome to js");
}
console.log(myDetails);
console.log(myDetails.greet);
************ constructor *********** *
Constructor functions technically are regular functions.There are two conventions though:
They are named with capital letter first.
They should be executed only with "new" operator.
Using constructor functions to create objects gives a great deal of flexibility.The constructor function may have parameters that define how to construct the object, and what to put in it.
For instance:
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User("Jack");
alert(user.name); // Jack
alert(user.isAdmin); // false
When a function is executed with new, it does the following steps:
A new empty object is created and assigned to this.
The function body executes.Usually it modifies this, adds new properties to it.
The value of this is returned.
add method in constructor
Of course, we can add to this not only properties, but methods as well.
function User(name) {
this.name = name;
this.sayHi = function () {
alert("My name is: " + this.name);
};
}
let john = new User("John");
john.sayHi(); // My name is: John
****** objects methos ****** *
an object property that include a function declaration is known as object method
types of object method
1) Object.freez():
changinfg the frozen object is impossible
it prevent the addition and deletion of property
also prevent changes
Object.freeze(john);
2) objet.seal():
you can not add or remove property but you can modify property
Object.seal(john);
A function which is associated with object called method
When a function is a property of an object, it becomes a method.
four ways to associate function to object
let person = {
name: "John",
age: 30,
}
person.sayHello = function () {
console.log("Hi! My Name Is ");
}
person.sayHello();
************ 2 *****************
let person = {
name: "John",
age: 30,
};
function greet() {
console.log("say hello");
}
person.sayHello = greet;
person.sayHello();
************* 3 *************
let person = {
name: "John",
age: 30,
sayHello: function () {
console.log("hello11");
}
};
person.sayHello();
************* 4 ***************
let person = {
name: "John",
age: 30,
sayHello() {
console.log("hello11");
}
};
person.sayHello();
------------------------------------------------------------------------------------------------
********** optional chaining **********
Optional chaining is a feature of javascript that allow you to access the properties of an object and element of an array without having to check the array and object is null or undefine first it is represent by?.operator and used to cociesly access deeply nested property without having to write long chain of if else statement
the optional chaining?.stops the evaluation if the value before?.is undefined or null and returns undefined.
**********example *********
let user = {}; // user has no address
alert(user?.address?.street); // undefined (no error)
The?.[] syntax also works, if we’d like to use brackets[] to access properties instead of dot..Similar to previous cases, it allows to safely read a property from an object that may not exist.
let key = "firstName";
let user1 = {
firstName: "John"
};
let user2 = null;
alert(user1?.[key]); // John
alert(user2?.[key]); // undefined
delete user?.name;
************ object destructuring ***********************
the destructurig assignment syntex is a javascript expression that make it possible to unpack from Array or property from object, into distinct variables.
let obj = {
name: 'ali',
age: 25,
city: {
country: 'USA',
state: 'California'
}
};
let { name, age, city } = obj;
console.log(`name is${name} and age is ${age} city is ${city.state}`);
************************* object properties***************************************************
Object properties have a three special property called flags
1)writable => if true, then value can be change otherwise read only.
2) enumberable => if true, then listed in loops otherwise not listed.
3) configurable => if true, then property can be deleted and its value can be modified.
When we create a property “the usual way”, all of them are true.But we also can change them anytime.
The method Object.getOwnPropertyDescriptor allows to query the full information about a property.
The syntax is:=>
let descriptor = Object.getOwnPropertyDescriptor(obj, propertyName);
obj => the object to get information from
propertyName => the name of the property.
example=>
let prop1 = {
name: "prop1",
value: 20
};
let descriptor = Object.getOwnPropertyDescriptor(prop1, "name");
console.log(JSON.stringify(descriptor, null, 2));
output => {
"value": "prop1",
"writable": true,
"enumerable": true,
"configurable": true
}
To change the flags, we can use Object.defineProperty.
The syntax is:=>
Object.defineProperty(obj, propertyName, descriptor)
For instance, here a property name is created with all falsy flags:
let user = {};
Object.defineProperty(user, "name", {
value: "John"
});
let descriptor = Object.getOwnPropertyDescriptor(user, 'name');
alert(JSON.stringify(descriptor, null, 2));
{
"value": "John",
"writable": false,
"enumerable": false,
"configurable": false
}
non - writable=>
Let’s make user.name non - writable(can’t be reassigned) by changing writable flag:
let user = {
name: "John"
};
Object.defineProperty(user, "name", {
writable: false
});
user.name = "Pete"; // Error: Cannot assign to read only property 'name'
non - enumbrable=>
we can set enumerable: false.Then toString won’t appear in a for..in loop, just like the built -in one:
let user = {
name: "John",
toString() {
return this.name;
}
};
Object.defineProperty(user, "toString", {
enumerable: false
});
// Now our toString disappears:
for (let key in user) alert(key); // name
non - configurable=>
The non - configurable flag(configurable: false) is sometimes preset for built -in objects and properties.
A non - configurable property can’t be deleted, its attributes can’t be modified.
For instance, Math.PI is non - writable, non - enumerable and non - configurable:
let descriptor = Object.getOwnPropertyDescriptor(Math, 'PI');
alert(JSON.stringify(descriptor, null, 2));
{
"value": 3.141592653589793,
"writable": false,
"enumerable": false,
"configurable": false
}
Please note: configurable: false prevents changes of property flags and its deletion, while allowing to change its value.
Object.defineProperties=>
There’s a method Object.defineProperties(obj, descriptors) that allows to define many properties at once.
The syntax is:=>
Object.defineProperties(obj, {
prop1: descriptor1,
prop2: descriptor2
// ...
});
For instance:=>
Object.defineProperties(user, {
name: { value: "John", writable: false },
surname: { value: "Smith", writable: false },
// ...
});
*/