forked from adamszaruga/es6
-
Notifications
You must be signed in to change notification settings - Fork 13
/
demo.js
56 lines (46 loc) · 1.16 KB
/
demo.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
// Let's say we need to coalesce these variables into one object:
let id = "1234";
let firstName = "Sue";
let lastName = "Geller";
let email = "sg@suegeller.com";
// This is how we would do it with ES5 Javascript:
let obj = {
id: id,
firstName: firstName,
lastName: lastName,
email: email,
passwordHash: passwordHash
}
// ^^^ Notice how redundant that was! ES6 gives us a short cut:
let obj2 = {
id,
firstName,
lastName,
email,
password
}
// ^^^ We're still creating an object literal with { }.
// The keys of this new object are the variable names themselves
// The values of the object are the values of those variables
// You're still free to add normal key/value pairs, too:
let obj3 = {
id,
firstName,
preferredEmail: email,
nickname: "Charlie"
}
// But wait, there's more! If your object has a key that points to a function,
// you can define your function inline, like so:
let obj3 = {
name: "Adam",
shout() {
return "AAAHHHHHH!!!"
}
}
obj3.shout();
// You can also dynamically add keys to object literals:
let keyToWrite = "key2";
let obj4 = {
[keyToWrite]: "hi"
}
obj4.key2 === "hi";