展开符和解构赋值

一、展开符

展开符(剩余操作符):...
1.展开符号

'use strict';
let arr_one = [1,3];
let arr_two = [4,5,...arr_one];
console.log(arr_one);//[ 1, 3 ]
console.log(...arr_one);//1 3
console.log(arr_two);//[ 4, 5, 1, 3 ]

2.剩余操作符(类似arguments)

'use strict';

function test(ss,...others){
    console.log(arguments);//{ '0': 1, '1': 2, '2': 3, '3': 4 }
    console.log(ss);//1
    console.log(others);//[ 2, 3, 4 ]
};
test(1,2,3,4);

一、解构赋值

解构赋值有种形式;

  1. 解构数组
'use strict';

function test(){
    return [1,2,3];
};
let [one,two,three] = test();
console.log(one,two,three);//1 2 3

2.解构对象
解构对象中定义的变量和常见的对象有所区别,在定义变量的时候,键值对中“键”对应的是要解构对象的属性,而“值”才是新定义的变量,如下面案例中的变量“one_test”才是新定义的变量,前面的“one”是test()返回对象的“one”属性;

'use strict';
function test(){
    return {'one':1,'two':2,'three':3};
};
let {one:one_test,two:two,three:three} = test();
console.log(one_test,two,three);//1 2 3

文末福利:

福利一:前端,Java,产品经理,微信小程序,Python等资源合集大放送:https://www.jianshu.com/p/e8197d4d9880
福利二:微信小程序入门与实战全套详细视频教程:https://www.jianshu.com/p/e8197d4d9880



作者:喜欢坑队友的程序员
链接:https://www.jianshu.com/p/5f3f8eaacc4f
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/wangting888/p/9701680.html