-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path056-enhanced-object-literals.js
More file actions
213 lines (172 loc) · 4.42 KB
/
056-enhanced-object-literals.js
File metadata and controls
213 lines (172 loc) · 4.42 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
205
206
207
208
209
210
211
212
213
// 1: TRADITIONAL OBJECT SYNTAX
const name = "John";
const age = 30;
const city = "New York";
// const person = {
// name: name,
// age: age,
// city: city,
// greet: function() {
// console.log("Hello, I'm " + this.name);
// }
// };
// console.log(person);
// 2: PROPERTY SHORTHAND
// const person = {
// name,
// age,
// city,
// greet: function() {
// console.log("Hello, I'm " + this.name);
// }
// };
// console.log(person);
// Output: { name: 'Sarah', age: 28, city: 'London' }
const createUser = (username, email, role) => {
return {
username,
email,
role,
createdAt: new Date()
};
};
const newUser = createUser("coder123", "coder@example.com", "admin");
console.log(newUser);
// 3: METHOD SHORTHAND
// Old way
const calculator = {
add: function(a, b) {
return a + b;
},
subtract: function(a, b) {
return a - b;
}
};
// ES6 Method Shorthand
const betterCalculator = {
add(a, b) {
return a + b;
},
subtract(a, b) {
return a - b;
},
multiply(a, b) {
return a * b;
}
};
console.log(betterCalculator.add(5, 3)); // 8
console.log(betterCalculator.multiply(4, 2)); // 8
const user = {
firstName: "Alex",
lastName: "Johnson",
getFullName() {
return `${this.firstName} ${this.lastName}`;
},
greet(timeOfDay) {
return `Good ${timeOfDay}, ${this.getFullName()}!`;
}
};
console.log(user.getFullName()); // "Alex Johnson"
console.log(user.greet("morning")); // "Good morning, Alex Johnson!"
// 4: COMPUTED PROPERTY NAMES
const propertyName = "score";
const prefix = "user";
const gameData = {
[propertyName]: 100,
[`${prefix}Name`]: "Player1",
[`${prefix}Level`]: 5
};
console.log(gameData);
// Output: { score: 100, userName: 'Player1', userLevel: 5 }
const createDynamicObject = (key, value) => {
return {
[key]: value,
[`${key}Uppercase`]: value.toUpperCase(),
[`is${key.charAt(0).toUpperCase() + key.slice(1)}Valid`]: true
};
};
const result = createDynamicObject("name", "javascript");
console.log(result);
// { name: 'javascript', nameUppercase: 'JAVASCRIPT', isNameValid: true }
// 5: COMBINING ALL ENHANCEMENTS
const buildProduct = (id, name, price, category) => {
const timestamp = Date.now();
const status = "active";
return {
// Property shorthand
id,
name,
price,
category,
status,
// Computed property names
[`${category}Product`]: true,
[timestamp]: "Created at this time",
// Method shorthand
getDetails() {
return `${this.name} - $${this.price}`;
},
applyDiscount(percentage) {
const discount = this.price * (percentage / 100);
return this.price - discount;
},
isAffordable(budget) {
return this.price <= budget;
}
};
};
const laptop = buildProduct(101, "Gaming Laptop", 1200, "electronics");
console.log(laptop);
console.log(laptop.getDetails()); // "Gaming Laptop - $1200"
console.log(laptop.applyDiscount(10)); // 1080
console.log(laptop.isAffordable(1500)); // true
// 6: PRACTICAL USE CASES
// Use Case 1: API Response Formatting
const formatAPIResponse = (data, statusCode, message) => {
const timestamp = new Date().toISOString();
return {
statusCode,
message,
timestamp,
data,
isSuccess() {
return this.statusCode >= 200 && this.statusCode < 300;
},
getFormattedData() {
return JSON.stringify(this.data, null, 2);
}
};
};
const response = formatAPIResponse({ users: [] }, 200, "Success");
console.log(response.isSuccess()); // true
// 7: IMPORTANT NOTES & GOTCHAS
// 1. Method Shorthand and Arrow Functions
const obj = {
// Method shorthand - 'this' works normally
regularMethod() {
console.log(this);
},
// Arrow function - 'this' is lexically bound
arrowMethod: () => {
console.log(this); // Will NOT refer to obj
}
};
// 2. Computed Property Names with Symbols
const uniqueKey = Symbol("id");
// const user = {
// name: "Tom",
// [uniqueKey]: 12345
// };
console.log(user[uniqueKey]); // 12345
// 3. You Can Mix Old and New Syntax
const mixed = {
// Shorthand
name,
age,
// Traditional
city: city,
// Method shorthand
greet() {
console.log("Hi!");
}
};