函数数组参数解构

来源:互联网 发布:淘宝上买春药搜什么 编辑:程序博客网 时间:2024/06/06 02:17

1.函数参数赋值新写法

老写法

function test(x,y){x=x||12,y=y||14console.log(x,y)}test(25)
新写法

function test (x=12,y=14){console.log(x,y)}test(25)//25 ,14

2.

function test ([x=2,y=3]=[]){console.log(x,y)}test()  //2,3  如果没有传递参数那么 函数部分需要写上 [x=2,y=3]=[]test ([,4]) //2,4test(undefined)//2,3test(null)//报错
3.

function test ([x=2,[y=3,z=4]=[]]=[],w=5){console.log(x,y,z,w)}test()  //2,3 ,4 ,5
4.

function test ([x=2,y=3]=[33,44]){console.log(x,y)}test()  //33,44test([]) //2,3

5.

function test ({x=111,y=222}){console.log(x,y)}//test() //报错test({}) //111,222 

6

function test({x,y}={x:12,y:22}){  console.log(x,y)}test({})//undefined undefinedtest() //12,22

7.

function test([x=12,y=22]=[],{name='ggb',age=28}={}){return {x,y,name,age}}// let [x,name,age ,y]=test();// console.log(x,y,name,age) //12 28 22 'ggb'let {x,name,age ,y}=test(); console.log(x,y,name,age) //12 22 'ggb' 28




原创粉丝点击