ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>展开语法(Spread syntax), 可以在函数调用/数组构造时, 将数组表达式或者string在语法层面展开;还可以在构造字面量对象时, 将对象表达式按key-value的方式展开。(译者注: 字面量一般指 [1, 2, 3] 或者 {name: "mdn"} 这种简洁的构造方式) ~~~ function sum(x, y, z) { return x + y + z; } const numbers = [1, 2, 3]; console.log(sum(...numbers)); // expected output: 6 console.log(sum.apply(null, numbers)); // expected output: 6 ~~~ 数组展开 ~~~ var arr=[1,2,3]; console.log(...arr); ~~~ >## 语法 函数调用: ~~~ myFunction(...iterableObj); ~~~ 字面量数组构造或字符串 ~~~ var arr0 = [1,2,3] var arr=[...arr0,..."hello"]; console.log(arr); ~~~ 构造字面量对象时,进行克隆或者属性拷贝 语法 ~~~ let objClone = { ...obj }; ~~~ ~~~ var obj = { name:"cheng", age:20 } var zhang = {...obj}; console.log(zhang); ~~~ ~~~ var parts = ['shoulders','knees']; var lyrics = ['head',... parts,'and','toes']; // ["head", "shoulders", "knees", "and", "toes"] ~~~ [mozllia文档](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax#%E8%AF%AD%E6%B3%95)