-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathES6.html
61 lines (52 loc) · 1.48 KB
/
ES6.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>ES6</title>
</head>
<body>
<h2>ES6 demo</h2>
<script>
/* ES5 */
function sum(x, y, z){
let total = 0;
if(x) total += x;
if(y) total += y;
if(z) total += z;
console.log(`total:${total}`)
}
sum(5, '', 9);
/* ES6 */
function sum2(...args){ // Rest参数
let total = 0;
for(var i of args){
total += i;
}
console.log(`total:${total}`)
}
sum2(4, 8, 10);
/* ES6 */
let sum3 = (...args) => { // Rest参数 + 箭头函数
let total = 0;
for(var i of args){
total += i;
}
console.log(`total:${total}`)
}
sum3(4, 8, 8);
var [x, y] = [4, 8]; // 解构赋值
console.log(...[4,8]); // 这里的...是数组扩展
let arr1 = [1,3]
let arr2 = [4, 8];
console.log("concat:" + arr1.concat(arr2));
console.log([...arr1, ...arr2]); // 这里的...是数组扩展
var [x, ...y] = [4, 8, 10, 30]; // Rest + 解构赋值
console.log(x, y);
let [a, b, c] = 'ES6'; // 字符串的解构
console.log(a, b, c);
let xy = [...'ES6']; // 数组的扩展
console.log(xy);
</script>
</body>
</html>