博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spread运算符_JavaScript Spread运算符
阅读量:2505 次
发布时间:2019-05-11

本文共 1910 字,大约阅读时间需要 6 分钟。

spread运算符

You can expand an array, an object or a string using the spread operator ....

您可以使用传播运算符...扩展数组,对象或字符串。

Let’s start with an array example. Given

让我们从一个数组示例开始。 给定

const a = [1, 2, 3]

you can create a new array using

您可以使用创建新数组

const b = [...a, 4, 5, 6]

You can also create a copy of an array using

您还可以使用创建一个数组的副本

const c = [...a]

This works for objects as well. Clone an object with:

这也适用于对象。 克隆对象:

const newObj = { ...oldObj }

Using strings, the spread operator creates an array with each char in the string:

使用字符串,spread运算符使用字符串中的每个字符创建一个数组:

const hey = 'hey'const arrayized = [...hey] // ['h', 'e', 'y']

This operator has some pretty useful applications. The most important one is the ability to use an array as function argument in a very simple way:

该运算符具有一些非常有用的应用程序。 最重要的一个功能是以一种非常简单的方式将数组用作函数参数的能力:

const f = (foo, bar) => {}const a = [1, 2]f(...a)

The rest element is useful when working with array destructuring:

在处理数组解构时, rest元素非常有用:

const numbers = [1, 2, 3, 4, 5]const [first, second, ...others] = numbers

and spread elements:

传播元素

const numbers = [1, 2, 3, 4, 5]const sum = (a, b, c, d, e) => a + b + c + d + econst result = sum(...numbers)

ES2018 introduces rest properties, which are the same but for objects.

ES2018引入了其余属性,但对象相同。

Rest properties:

休息性质

const { first, second, ...others } = {  first: 1,  second: 2,  third: 3,  fourth: 4,  fifth: 5}first // 1second // 2others // { third: 3, fourth: 4, fifth: 5 }

Spread properties allow to create a new object by combining the properties of the object passed after the spread operator:

传播属性允许通过组合在传播操作符之后传递的对象的属性来创建新对象:

const items = { first, second, ...others }items //{ first: 1, second: 2, third: 3, fourth: 4, fifth: 5 }

It is also the perfect way to merge two simple objects into one:

这也是将两个简单对象合并为一个的理想方法:

const object1 = {  name: 'Flavio'}const object2 = {  age: 35}const object3 = {...object1, ...object2 }

翻译自:

spread运算符

转载地址:http://dtqgb.baihongyu.com/

你可能感兴趣的文章
常用的20个强大的 Sublime Text 插件
查看>>
ajaxfileupload.js在IE中的支持问题
查看>>
tensorflow学习之(十)使用卷积神经网络(CNN)分类手写数字0-9
查看>>
当document.write里含有script标签时
查看>>
工作中常见问题
查看>>
JAVA 从一个List里删除包含另一个List的数据
查看>>
外国的月亮比较圆吗?外籍团队工作有感
查看>>
CentOS 关闭烦人的屏保
查看>>
分布式系统事务一致性解决方案
查看>>
ShuffleNet总结
查看>>
前后台验证字符串长度
查看>>
《算法导论 - 思考题》7-1 Hoare划分的正确性
查看>>
IOS 简单的动画自定义方法(旋转、移动、闪烁等)
查看>>
图像处理笔记(十二)
查看>>
Chapter 3 Phenomenon——9
查看>>
win64 Python下安装PIL出错解决2.7版本 (3.6版本可以使用)
查看>>
获取各种类型的节点
查看>>
表达式求值-201308081712.txt
查看>>
centos中安装tomcat6
查看>>
从Vue.js窥探前端行业
查看>>