-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced-object-literals.js
More file actions
41 lines (38 loc) · 979 Bytes
/
enhanced-object-literals.js
File metadata and controls
41 lines (38 loc) · 979 Bytes
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
//usual way to pass variables in ES5
function getName(name, age, year) {
return {
Name: name,
Age: age,
Year: year,
};
}
console.log(getName("Rakib", 25, 1996));
// in the above function, the object that is being returned is created using object literals.
//ES6 removes all of that repetition
function getCars(make, model, year) {
return {
make,
model,
year,
sayModel() {
return model;
},
};
//Note that if no variable has the same name as the property key defined, we'll get an error
}
console.log(getCars("Zetsu", "25CV", 2021));
console.log(getCars("Apple", "MacBook", "2015").sayModel());
console.log("*************************");
const person = (name) => {
return {
name,
age(x1, x2) {
return x1 + x2;
//The : and function are no longer necessary to define a method.
},
};
};
const info = person("Rakib");
console.log(info.name);
console.log(info.age(40, 10));
// console.log(person.("Rakib"));