标签:函数参数 console es6 blog code ons 默认 sum define
函数参数的解构赋值
function sum(x, y) { return x + y; } sum(1,2);//3 //解构赋值 function sum([x, y]) { return x + y; } console.log( sum([1,2]) );//3
函数参数解构赋值的默认值
function fun({x = 0, y = 0} = {}) { return [x, y]; } console.log( fun({}) ); //[0,0] console.log( fun() ); //[0,0] console.log( fun({x: 100}) ); //[100,0] console.log( fun({x: 100, y: 200}) ); //[100,200]
函数参数的解构赋值的默认值undefined
function fun({x, y} = { x:0, y:0 }) { return [x, y]; } console.log( fun({}) ); //[undefined,undefined] console.log( fun() ); //[0,0] 没传参数实际上是对象的解构赋值 console.log( fun({x: 100}) ); //[100,undefined] console.log( fun({x: 100, y: 200}) ); //[100,200]
标签:函数参数 console es6 blog code ons 默认 sum define
原文地址:http://www.cnblogs.com/jewave/p/6238606.html