ECMAScript 6 入门
本文简要介绍 ECMAScript 6 新引入的部分语法特性,作为入门教程的精髓。
let和const
变量的解构赋值
1 2 3 4 5 6
| let [a, b, c] = [1,2,3];
let {a, b} = {a: 'hello', b: 'world'}
let {a = 1 } = {};
|
字符串扩展
1 2 3 4 5 6 7
| $('#list').html(` <ul> <li>first</li> <li>second</li> </ul> `);
|
函数的扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| function log(x = 10, x = 20) { console.log(x, y); } log()
function add(...values) { let sum = 0; for (var val of values) { sum += val; } return sum; } add(2, 5, 3)
const f = v => v;
|
数组扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| console.log(...[1, 2, 3])
let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }; let arr2 = Array.from(arrayLike);
Array.of(3, 11, 8)
|