-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjects.js
More file actions
56 lines (40 loc) · 1.52 KB
/
Copy pathObjects.js
File metadata and controls
56 lines (40 loc) · 1.52 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
// Let's build our concepts on Objects
// To understand the importance of Objects in JS we can say that. If you wannna lean JS learn Objects and events
// in depth
// Now there are three ways to declear objects in JS.
// singleton
// You can also create objects by another method called constructor method and singleton will also made inside this
// Object.create;
// object literals
const JsUser = {
name: "Muhammad Shakir",
age: 22,
location: "Karachi",
email: "Shakir@google.com",
isLogedIn: false,
lastLoginDays: ['Monday', 'Friday', 'Sunday']
};
// Now how to access Object values?
// There are two ways by which you can access the object keys.
console.log(JsUser.name);
// But the issue on the above method is that its not working in all the cases.
// Lets do an interview question here
// Declear a symbol outside the object and use that symbol inside your object.
const mySym = Symbol("key1");
const newUser = {
name: "Rehan Sattar",
"GitHub usrname": "Rehan-Sattar.dev",
[mySym] : 77
}
// now to access the GitHub username there is no chance to print there value by using the above mehtod.
// So for this we have
console.log(newUser["GitHub usrname"]);
console.log(typeof newUser[mySym]);
// if you want to freez your object like you dont want/allow anyone to change object values.
Object.freeze(newUser);
console.log(newUser);
// lets create a function inside our object.
JsUser.greeting = function () {
console.log(`Hello JS User ${this.name}`);
}
console.log(JsUser.greeting());