💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 一、概述 三个点(...)叫扩展运算符,是在ES6中新增加的内容,它可以在函数调用/数组构造时,将数组表达式或者string在语法层面展开;还可以在构造字面量对象时将对象表达式按照key-value的方式展开; >[danger] 说白了就是把外面的包裹脱了,不管是大括号([])、花括号({}),统统不在话下,**全部脱掉脱掉**; ## 二、作用 ### (1)**复制** ~~~ //数组的复制 var arr1 = ['hello'] var arr2 =[...arr1] arr2 // ['hello'] //对象的复制 var obj1 = {name:'chuichui'} var obj2 ={...arr} ob12 // {name:'chuichui'} ~~~ ### (2)**合并** ~~~ //数组的合并 var arr1 = ['hello'] var arr2 =['chuichui'] var mergeArr = [...arr1,...arr2] mergeArr // ['hello','chuichui'] // 对象分合并 var obj1 = {name:'chuichui'} var obj2 = {height:176} var mergeObj = {...obj1,...obj2} mergeObj // {name: "chuichui", height: 176} ~~~ ### (3)**字符转数组** ~~~ var arr1 = [...'hello'] arr1 // ["h", "e", "l", "l", "o"] ~~~ ### (4)**函数传参** ~~~ function f(v,w,x,y,z){ } var args = [2,3] f(1,...args,4,...[5]) ~~~ ~~~ function f(x,y,z){} var args = [1,2,3] f(...args) // 以前的方法 f.apply(null,args); ~~~ ### (5)**解构变量** 解构数组: ~~~ let [melon, ...fruits ] = ['?', '?', '?', '?']; melon; //? fruits; // [ '?', '?', '?'] ~~~ 解构对象: ~~~ let user = {name : "Ram", age: 20, salary: '20K', job : "Tester" }; let { name, age, ...details } = user; name; // Ram age; // 20 details; // {salary: '20K', job : 'Tester'}; ~~~ ### (6)**将NodeList对象转换为数组** ~~~ let nodeList = document.querySelectorAll('.class') var nodeArray = [...nodeList] ~~~