解構(gòu)賦值的應(yīng)用:
1、變量交換
var x = 1
var y = 2
var [x, y] = [2, 1]
2、函數(shù)值返回
function f() {
return [1, 2]
}
3、Json 對(duì)象獲取
obj = {
a: 'a',
b: [1, 2, 3],
c: {
a: '1',
b: '2'
}
}
var {a, b, c} = obj
4、函數(shù)參數(shù)的默認(rèn)設(shè)置
Jquery.ajax = function (url,{
async = true,
beforeSend = function () {},
cache = true,
complete = function () {},
crossDomain = fase,
global = true
}) {
// ... do somthing
}
5、遍歷Map 結(jié)構(gòu)
const map = new Map()
map.set('first', 'hello')
map.set('second', 'world')
for(let [key, value] of map) {
console.log(key + "is" + value)
}
可以只取鍵值名,或者只去鍵值
for (let [key] of map) {}
for(let [,value] of map) {}
6、輸入模塊的制定方法
const { SourceMapConsumer, SourceNode} = require("source-map")
7、參數(shù)的定義
// 參數(shù)為有次序的值
function f([x,y,x]){}
f([1,2,3])
// 參數(shù)為無(wú)序的值
function f({x,y,z}){}
f({x:1,y:2,z:3})