-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathes6_destructing_demo.js
52 lines (40 loc) · 1.42 KB
/
es6_destructing_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
const log = require('./lib/util_for_node')
// ES5
var pl = {
name: 'Kotlin',
typeSystem: 'Static Type',
platform: 'JVM,JS,Android,Native'
}
var name = pl.name
var typeSystem = pl.typeSystem
var platform = pl.platform
log(name)
log(typeSystem)
log(platform)
const scope = () => { // avoid 上面的 var 变量声明的作用域冲突
// ES6
const pll = {
name: 'Kotlin',
typeSystem: 'Static Type',
platform: 'JVM,JS,Android,Native'
}
let {name, typeSystem, platform} = pll // destructing
log(name)
log(typeSystem)
log(platform)
}
scope()
/**
With ES5, we have to assign each value to each variable. With ES6, we just put our values within curly brackets to get any property of the object.
Note: if you assign a variable that is not identical to the name of property, it will return undefined. For example, if the name of the property is name and we assign it to a username variable, it will return undefined.
We always have to name the variable the same as the name of the property. But in case we want to rename the variable, we can use the colon : instead.
*/
// For the array, we use the same syntax as the object. We have just to replace the curly brackets with square brackets.
const plArray = ['Kotlin', 'Java', 'JavaScript', 'Scala', 'Python', 'Lisp'] // array
let [v1, v2, v3, v4, v5, v6] = plArray // Attention: here is []
log(v1)
log(v2)
log(v3)
log(v4)
log(v5)
log(v6)