Skip to content

Commit f1e136e

Browse files
committed
complete enhanced object literals
1 parent 2187c3a commit f1e136e

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

09-Data-Structure-Modern-Operators-and-Strings/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,47 @@ for (const bar of newMenu2.entries()) {
221221

222222
console.log([...newMenu2.entries()]);
223223
```
224+
225+
# Enhanced Object Literals
226+
227+
Object literals are objects written literally, using the curly braces syntax. With ES6, enhanced object literals introduced three ways that allows you write object literals in an easy way.
228+
229+
It provides a shorthand syntax for initializing properties from variables.
230+
It provides a shorthand syntax for defining function methods.
231+
It enables the ability to have computed property names in an object literal definition
232+
233+
For Example.
234+
Consider the object below which has two methods and nested objects. With enhanced object literal, we don't need to assign a key: and it's value pair, we simply just write the object name.
235+
236+
```js
237+
const testing = {
238+
thu: {
239+
open: 12,
240+
close: 22,
241+
},
242+
fri: {
243+
open: 11,
244+
close: 23,
245+
},
246+
};
247+
248+
const other = {
249+
// ES5
250+
testing: testing,
251+
// ES6
252+
testing,
253+
254+
// ES5
255+
orderPizza: function (mainIngredient, ...otherIngredients) {
256+
console.log(mainIngredient, otherIngredients);
257+
},
258+
// ES6
259+
orderPizza(mainIngredient, ...otherIngredients) {
260+
console.log(mainIngredient, otherIngredients);
261+
},
262+
};
263+
```
264+
265+
Here we removed the colon `:` and the property `name` and simply passed in the object as a variable, and wrote the function like a regular function without the function keyword and colon.
266+
267+
These all balls down to personal preference. You can choose which one you want to use.

09-Data-Structure-Modern-Operators-and-Strings/script.js

+28
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,31 @@ for (const [i, el] of newMenu2.entries()) {
285285
// console.log(item);
286286
console.log(`${i + 1}: ${el}`);
287287
}
288+
289+
// Enhanced object literals
290+
const testing = {
291+
thu: {
292+
open: 12,
293+
close: 22,
294+
},
295+
fri: {
296+
open: 11,
297+
close: 23,
298+
},
299+
};
300+
301+
const other = {
302+
// ES5
303+
testing: testing,
304+
// ES6
305+
testing,
306+
307+
// ES5
308+
orderPizza: function (mainIngredient, ...otherIngredients) {
309+
console.log(mainIngredient, otherIngredients);
310+
},
311+
// ES6
312+
orderPizza(mainIngredient, ...otherIngredients) {
313+
console.log(mainIngredient, otherIngredients);
314+
},
315+
};

0 commit comments

Comments
 (0)