Destructing
这个应该是翻译成“解构”吧 ES6允许这样这样操作
首先是对于Object
var obj = { a:"Hello", b:"Hi" }
var {a,b} = obj //Bingo, now a = "Hello", b = "Hi"var {a,c} = obj //Wrong, now a = "Hello", c = undefined
对于数组,效果类同
var arr = [ "Hello", "Hi" ]
var [a,b] = arr //Bingo, now a = "Hello", b = "Hi"var [a,c] = arr //Also correct, now a = "Hello", b = "Hi"
Spread Operator
翻译为“展开运算符”, 语法是三个点: …
这是一个仅针对Array有效的运算符,作用为在那些需要同时调用多个参数/元素 的地方提供语法便利
function sum(x,y,z){
return x+y+z
}
console.log(sum(...[1,2,3])) //6console.log(sum(...[1,2,3,4,5])) //6console.log(sum(...[1,,3,4,5])) //NaNvar doSomething = function(...args){
return sum(..args)
}
console.log(doSomething(...[1,2,3])) //6//对Object是否有效呢?var obj = { a:"A", b:"B", c:"C", d:"D", e:"E" }
var {a,b,...others} = obj //报错