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
let [a, b, c] = [ 1, 2, 3] let [foo, [[bar], baz]] = [ 1, [[ 2], 3]] let [ , , third] = [ 'one', 'two', 'three'] let [head, ...tail] = [ 1, 2, 3, 4] // 默认值 匹配到undeined使用默认值 let [foo = true] = [] // 默认值可以用表达式 function () {} let [x = f()] = [ 1] // 对象键值匹配 let {foo, bar} = { foo: 1, bar: 2} let { foo: f, bar: b} = { foo: 1, bar: 2} //这里()是必须的,没有的话{}会被解析成代码块 let foo ({foo} = { foo: 1})
1 2 3 4 5
let x = 1 let y = 2 [x, y] = [y, x]
1 2 3 4 5 6 7 8 9 10 11 12
let jsonData = { id: 42, status: "OK", data: [ 88, 10] } let { id , status, data: number} = jsonData console.log(id, status, number) //42, "OK", [88,10]
jQuery.ajax = function (url, { async = true, beforeSend = function () {}, cache = true, complete = function () {}, crossDomain = false, globa = true, // ... more config }) { // ... do something }
1 2 3 4 5 6 7 8
let map = new Map() map.set( 'first', 'hello') map.set( 'second', 'world') for ( let [key, value] of map) { console.log(key + " is " + value) }
1 2 3
'12'.padStart( 10, 'YYYY-MM-DD') //'YYYY-MM-12' '12'.padEnd( 10, 'YYYY-MM-DD') //'12YY-MM-DD'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
let name = "Bob" let time = "today" `Hello ${name}, how are you ${time}?` // 执行模板 let str = 'return ' + '`Hello ${name}!`' let func = new Function( 'name', str) func( 'Jack') //"Hello Jack!" let str = '(name) => `Hello ${name}!`' let func = eval.call( null,str); func( 'Jack') //"Hello Jack!"
1 2 3 4 5 6 7
let a = 5; let b = 10; tag `Hello ${ a + b } world ${a * b}` // 等同于 tag([ 'Hello ', ' world', ''], 15, 50)
原文:大专栏 ECMAScript6 学习笔记 一