-
Notifications
You must be signed in to change notification settings - Fork 5
/
12_objects.js
138 lines (126 loc) · 5.32 KB
/
12_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
function objects() {
/**
* ========================================================
* Object Literals
* ========================================================
* An object literal is the simplest way to create an object.
* Objects can have properties and methods (functions as properties).
*/
const person = {
name: "Ishtmeet",
age: 30,
greet() {
return `Hello, ${this.name}`;
},
};
console.log(person.greet()); // Outputs: "Hello, Ishtmeet"
/**
* ========================================================
* Accessing Properties
* ========================================================
* Properties can be accessed using dot notation or bracket notation.
* Dot notation is more readable and is generally used when the property name is known ahead of time.
*/
console.log(person.name); // Dot notation
console.log(person["age"]); // Bracket notation
/**
* ========================================================
* Adding & Updating Properties
* ========================================================
* New properties can be added and existing properties can be updated.
* This showcases the dynamic nature of JavaScript objects.
*/
person.email = "[email protected]";
person["age"] = 31;
console.log(person); // Outputs updated object
/**
* ========================================================
* Deleting Properties
* ========================================================
* The `delete` keyword is used to remove properties from an object.
* Note that this operation is not reversible.
*/
delete person.email;
console.log(person); // Outputs object without 'email' property
/**
* ========================================================
* Object Constructor
* ========================================================
* The `new Object()` constructor is another way to create an object.
* While less commonly used than object literals, it is equally powerful.
*/
const car = new Object();
car.brand = "Tesla";
car.model = "Model S";
console.log(car); // Outputs the car object
/**
* ========================================================
* Object Methods
* ========================================================
* Methods are functions that are stored as object properties.
*/
car.describe = function () {
return `${this.brand} ${this.model}`;
};
console.log(car.describe()); // Outputs: "Tesla Model S"
/**
* ========================================================
* Object Destructuring
* ========================================================
* Destructuring allows you to extract properties into variables.
* This feature was introduced in ES6 and is often used for better readability.
*/
const { name, age } = person;
console.log(name, age); // Outputs: "Ishtmeet 31"
/**
* ========================================================
* Looping Over Properties
* ========================================================
* The `for...in` loop can iterate through object keys.
* It is a good practice to use `hasOwnProperty()` to filter out properties from the prototype chain.
*/
for (const key in person) {
if (person.hasOwnProperty(key)) {
console.log(`${key}: ${person[key]}`);
}
}
/**
* ========================================================
* Checking Property Existence
* ========================================================
* There are multiple ways to check for property existence.
* `in` checks for own and inherited properties, `.hasOwnProperty()` checks for own properties.
*/
console.log("name" in person); // Outputs: true
console.log(person.hasOwnProperty("email")); // Outputs: false
/**
* ========================================================
* Object Spread Operator
* ========================================================
* The spread operator can clone or merge objects.
* It is a quick and easy way to perform these operations but only does shallow cloning.
*/
const clonePerson = { ...person };
console.log(clonePerson); // Outputs a clone of 'person'
/**
* ========================================================
* Object.assign()
* ========================================================
* `Object.assign()` method can also be used to clone or merge objects.
* It provides more control and is generally used for more complex operations.
*/
const anotherClone = Object.assign({}, person);
console.log(anotherClone); // Outputs another clone of 'person'
/**
* ========================================================
* Object.freeze(), Object.seal()
* ========================================================
* These methods control object mutability.
* `Object.freeze()` makes an object immutable.
* `Object.seal()` prevents new properties from being added and existing ones from being deleted, but allows value modification.
*/
Object.freeze(person);
person.age = 32; // Won't work because the object is frozen
console.log(person.age); // Outputs: "31"
}
objects();