Skip to content

Commit fd11deb

Browse files
committed
Additional inbuilt object methods
1 parent ad5fb04 commit fd11deb

File tree

3 files changed

+158
-10
lines changed

3 files changed

+158
-10
lines changed

8_Objects_in_detail/object.js

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,93 @@
1515
// console.log(person);
1616

1717
// Dot notation
18-
const person = {
19-
firstName: "Shubham",
18+
// const person = {
19+
// firstName: "Shubham",
20+
// };
21+
22+
// person.dog = {
23+
// name: "fluffy",
24+
// age: 2,
25+
// };
26+
27+
// person.age = 23;
28+
// console.log(person.dog.age);
29+
30+
// Square bracket notation
31+
// const property = "age";
32+
33+
// console.log(person[property]);
34+
35+
// Object.keys() creates an array containg the keys of an object.
36+
37+
// Intialize an object
38+
const employee = {
39+
boss: "Shubham",
40+
secretary: "Samarth",
41+
sales: "John",
42+
accountant: "Oscar",
2043
};
2144

22-
person.dog = {
23-
name: "fluffy",
24-
age: 2,
45+
// Let's say we want to see all the positions(keys) in the employee object
46+
const positions = Object.keys(employee);
47+
console.log(positions);
48+
49+
// Object.values() creates an array containg the values of an object.
50+
const session = {
51+
id: 1,
52+
time: `26-July-2022`,
53+
device: "Mobile",
54+
browser: "Chrome",
2555
};
2656

27-
person.age = 23;
28-
console.log(person.dog.age);
57+
const sessionInfo = Object.values(session);
58+
console.log(sessionInfo);
2959

30-
// Square bracket notation
31-
const property = "age";
60+
// Object.entries() creates an array containg the key/value of an object.
61+
const operatingSystem = {
62+
name: "Ubuntu",
63+
version: "20.04",
64+
license: "Open Source",
65+
};
66+
67+
const entries = Object.entries(operatingSystem);
68+
69+
entries.forEach((entry) => {
70+
let key = entry[0];
71+
let value = entry[1];
72+
73+
console.log(`${key}:${value}`);
74+
});
75+
76+
// Object.freeze() prevents modification to
77+
// properties and values of an object,
78+
// and prevents properties from being
79+
// added or removed from an object
80+
81+
// Intialize an object
82+
// const user = {
83+
// username: "Shubham",
84+
// password: "12345",
85+
// };
86+
87+
// const admin = Object.freeze(user);
88+
89+
// user.username = "Samarth"; // Trying to overwrite the object username
90+
// console.log(user.username); // Shubham (remains the same);
91+
92+
// Object.seal() prevents new properties
93+
// from being added to an object,
94+
// but allows the modification of existing properties
95+
// Intialize an object
96+
97+
const user = {
98+
username: "Shubham",
99+
password: "12345",
100+
};
101+
102+
const newUser = Object.seal(user);
103+
104+
newUser.username = "Samarth"; // The username will be changed
105+
newUser.age = 23; // the age property will not be added because we applied Object.seal()
32106

33-
console.log(person[property]);
107+
console.log(user);

8_Objects_in_detail/objects.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,76 @@ const car = {
6464
car.Details(); // Lambo 2019
6565
```
6666

67+
#### Methods
68+
69+
```js
70+
// Object.keys() creates an array containg the keys of an object.
71+
72+
// Intialize an object
73+
const employee = {
74+
boss: "Shubham",
75+
secretary: "Samarth",
76+
sales: "John",
77+
accountant: "Oscar",
78+
};
79+
80+
// Let's say we want to see all the positions(keys) in the employee object
81+
const positions = Object.keys(employee);
82+
console.log(positions); // [ 'boss', 'secretary', 'sales', 'accountant' ]
83+
84+
// Object.values() creates an array containg the values of an object.
85+
const session = {
86+
id: 1,
87+
time: `26-July-2022`,
88+
device: "Mobile",
89+
browser: "Chrome",
90+
};
91+
92+
const sessionInfo = Object.values(session);
93+
console.log(sessionInfo); // [ 1, '26-July-2022', 'Mobile', 'Chrome' ]
94+
95+
// Object.entries() creates an array containg the key/value of an object.
96+
const operatingSystem = {
97+
name: "Ubuntu",
98+
version: "20.04",
99+
license: "Open Source",
100+
};
101+
102+
const entries = Object.entries(operatingSystem);
103+
104+
console.log(entries); // [
105+
[ 'name', 'Ubuntu' ],
106+
[ 'version', '20.04' ],
107+
[ 'license', 'Open Source' ]
108+
]
109+
110+
// Intialize an object
111+
const user = {
112+
username: "Shubham",
113+
password: "12345",
114+
};
115+
116+
const admin = Object.freeze(user);
117+
118+
user.username = "Samarth"; // Trying to overwrite the object username
119+
console.log(user.username); // Shubham (remains the same);
120+
121+
// Object.seal() prevents new properties
122+
// from being added to an object,
123+
// but allows the modification of existing properties
124+
// Intialize an object
125+
126+
const user = {
127+
username: "Shubham",
128+
password: "12345",
129+
};
130+
131+
const newUser = Object.seal(user);
132+
133+
newUser.username = "Samarth"; // The username will be changed
134+
newUser.age = 23; // the age property will not be added because we applied Object.seal()
135+
136+
console.log(user);// { username: 'Samarth', password: '12345' }
137+
138+
```
139+

8_Objects_in_detail/objects2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ const car = {
1717
};
1818

1919
car.Details(); // Lambo 2019
20+

0 commit comments

Comments
 (0)