介绍

Rest 和 Spread 的共同点是语法都是 ... (点点点). 但它们的概念是相反的. 看例子体会:

Rest Parameters

参考: 阮一峰 – rest 参数

rest parameters 可以替代 arguments 对象.

old school:

function method(v1, v2, v3) {
console.log(arguments[0]);
console.log(arguments[1]);
console.log(arguments[2]);
console.log(arguments[3]); // 4
console.log(arguments.length); // 4
console.log(Array.isArray(arguments)); // false
// arguments is iterable
for (const arg of arguments) {
console.log('arg', arg);
}
} method(1, 2, 3, 4);

modern:

function method(...args) {
console.log(Array.isArray(args)); // true
}
method(1, 2, 3, 4);

除了它是 array 以外, 其它都和 arguments 一样,

left arguments

除了可以取代 arguments 对象, 它还可以更灵活.

function method(firstArg, ...leftArgs) {
console.log(firstArg); // 1
console.log(leftArgs); // [2, 3, 4]
}
method(1, 2, 3, 4);

直接把 arguments 拆成  2 组, 是不是很方便?

注: rest parameters 只能放到最后一个 parameter 哦, 下面这样放中间是不行的哦

use in destructuring assignment

const [v1, ...others] = [1, 2, 3, 4];

和 left arguments 同个概念, 只是用在 destructuring assignment 上. (rest 依然必须在最后一个哦)

解构对象也是可以的

const { name, ...leftProperties } = { name: 'Derrick', age: 11 };
console.log(leftProperties.age); // 11

Spread Operator

参考:

阮一峰 – 数组扩展运算符

阮一峰 – 对象扩展运算符

spread operator 可以用在 array 和 object 上, 先看看 array 的用法.

spread array to parameters

function method(a, b, c) {}

method(1, 2, 3);
method(...[1, 2, 3]);
method(1, ...[2, 3]);

rest parameters 是把多个值 combine 到一个 array 里. spread operator 有点相反的味道.

它把一个 array 拆成多个 parameters 输入.

spread array fill into array

const array1 = [1, 2, 3];
const array2 = [...array1, 4, 5]; // [1, 2, 3, 4, 5]

array1 被拆成一个一个放入新的 array 中.

如果 array 是空的, 那什么也不会发生.

const array3 = [...[], 4, 5]; // [4, 5]

spread object fill into object

这个是 ES2018 才有哦.

和 fill into array 玩法一样

const obj1 = { age: 11 };
const obj2 = {
name: 'Derrick',
age: 10,
...obj1,
level: 50,
};
// obj2 = { name: 'Derrick', age: 11, level: 50 }

obj1.age 11 覆盖掉了原本的 obj2.age 10, 上面这个写法和 Object.assign 是等价的哦.

小心 undefined

const obj1 = {
name: undefined,
}; const obj2 = {
name: 'Derrick',
...obj1,
}; console.log(obj2.name); // undefined

Object.assign 也会这样哦. 所以要特别小心. 有 2 个解决方法:

第一是针对属性做判断

const obj1 = {
name: undefined,
}; const obj2 = {
name: 'Derrick',
...(obj1.name !== undefined ? { name: obj1.name } : undefined),
};
console.log(obj2.name); // 'Derrick'

当 obj1.name 有值就变成 ...{ name: obj1.name } 不然就是 ...undefined, 然后 undefined 会被强转成 empty object 最终就是 ...{} 啥也没有就抵消掉了.

另一个方法是自己封装一个 Object.Assign, 自己 loop key 检查是否是 own property 才 override.

总结

Rest Parameters 像 "收", 把多个值收集到一个 array 或 object 中.

Spread Operator 像 "放" 把 array 或 object 里的值, 释放开来.

可以同时 "放" 多个, 但是不能 "收" 多个.

const a = [1, 2];
const b = [4, 5];
const c = [...a, 3, ...b]; // 同时释放 2 个 const [...a, 3, ...b] = [1, 2, 3, 4, 5]; // error! 同时收 2 个是不可以的 (因为它不知道要 combine 多少个丫)

JavaScript – Rest Parameters & Spread Operator的更多相关文章

  1. JavaScript展开操作符(Spread operator)介绍

    本文介绍JavaScript的展开操作符(Spread operator)....本文适合ES6初学者. 你可以通过展开操作符(Spread operator)...扩展一个数组对象和字符串.展开运算 ...

  2. JavaScript剩余操作符Rest Operator

    本文适合JavaScript初学者阅读 剩余操作符 之前这篇文章JavaScript展开操作符(Spread operator)介绍讲解过展开操作符.剩余操作符和展开操作符的表示方式一样,都是三个点 ...

  3. 《理解 ES6》阅读整理:函数(Functions)(三)Function Constructor & Spread Operator

    增强的Function构造函数(Increased Capabilities of the Function Constructor) 在Javascript中Function构造函数可以让你创建一个 ...

  4. [ES6] 13. Using the ES6 spread operator ...

    The spread operator (...) allows you to "explode" an array into its individual elements. S ...

  5. [ES6] 23. Rest Parameters & Spread Parameters

    Rest Parameters: In ES5, when you don't know how many paramters will be passed in, you can use argum ...

  6. [译]Javascript中的Ternary operator

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  7. [Python] Object spread operator in Python

    In JS, we have object spread opreator: const x = { a: '1', b: '2' } const y = { c: '3', d: '4' } con ...

  8. [ES6系列-06]展开操作符 Spread Operator 就像解压到这里

    [原创]码路工人 Coder-Power 大家好,这里是码路工人有力量,我是码路工人,你们是力量. github-pages 博客园cnblogs 在前面的文章中,介绍了...在获取剩余参数中的作用. ...

  9. [ES6] ... spread operator

    var parts = ['shoulders', 'knees']; var lyrics = ['head', ...parts, 'and', 'toes']; // ["head&q ...

  10. 现在就可以使用的5个 ES6 特性

    小编推荐:掘金是一个高质量的技术社区,从 ECMAScript 6 到 Vue.js,性能优化到开源类库,让你不错过前端开发的每一个技术干货.各大应用市场搜索「掘金」即可下载APP,技术干货尽在掌握. ...

随机推荐

  1. ArchLinux Vmware安装指北

    ArchLinux Vmware安装指北 在本文开始之前,首先允许我提前声明一点,Arch Linux的安装并不算难,但是绝对也算不上简单,中间的安装可能会遇到很多问题,本篇文章不能保证完全贴合你的真 ...

  2. abc--cf训练日常总结

    ABC 最近遇到好多思维和位运算的题目不会做,特地过来总结一些小小的知识点. 思维题目 https://atcoder.jp/contests/abc353/tasks/abc353_c 这道题目要求 ...

  3. 【教程】解决npm 报错 npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

    问题描述 只要在控制台执行npm,不论有没有参数,都会有此警告: npm WARN config global `--global`, `--local` are deprecated. Use `- ...

  4. uniapp打包所需的ios证书和证书profile文件获取的图文教程

    使用uniapp进行云打包,可以打包android和ios两种app,但是uniapp官方并不能凭空产生这两种平台所需的打包证书. 那么这两种打包证书又是如何获取呢? android相对简单,使用jd ...

  5. Jmeter函数助手39-isPropDefined

    isPropDefined函数用于判断属性是否存在. 变量的名称:填入属性名.如果属性名存在返回true,如果不存在返回false 1.jmeter的属性查看路径:测试计划右键"添加&quo ...

  6. 【SpringMVC】03 使用注解

    第一步还是配置web.xml,使用分发器统一处理请求和加载容器文件 <?xml version="1.0" encoding="UTF-8"?> & ...

  7. 【REGX】正则表达式 选中空白行

    参考地址: https://www.cnblogs.com/peijyStudy/p/13201576.html VScode并列替换不够智能,我需要等行粘贴,结果SHIFT+ALT复制内容粘贴上去就 ...

  8. OneFlow计算框架的OneAgent是不是一个子虚乌有的东西?

    自己是搞强化学习的,今天看了些OneFlow计算框架的一些资料,发现OneFlow官方一直有宣传自己的强化学习框架--OneAgent,但是十分诡异的是从了OneFlow的官方宣传可以看到这个词,但是 ...

  9. 什么样的AI计算框架才是受用户喜欢的?

    说明,本文是个人的一些胡想. 背景: AI计算框架现在从国外的百家争鸣过度到了国内百家争鸣的局面了.在7.8年前的时候,国外的AI计算框架简直是数不胜数,从14.15年前Nvidia公司的显卡需要手动 ...

  10. 使用Linux桌面壁纸应用variety发现的一些问题

    本人Ubuntu18.04 Desktop系统安装桌面壁纸应用variety,设置如下: 使用大致两个小时,主机为NVIDIA显卡,查看显存使用情况: 可以发现随着使用时间的增加variety会逐渐增 ...