Skip to Content
JavaScript 经典实例:第三版
book

JavaScript 经典实例:第三版

by Adam D. Scott, Matthew MacDonald, Shelley Powers
September 2022
Intermediate to advanced
585 pages
8h 17m
Chinese
China Electric Power Press Ltd.
Content preview from JavaScript 经典实例:第三版
114
5
5.5.3
讨论
展开运算符把一个数组展开成一组元素。严格来说,展开运算符可操作任何可迭代
的对象,包括其他集合类型。本章有好几节用到展开运算符。
展开运算符不一定为函数提供全部参数,甚至可以不是最后一组参数。像下面这样
使用完全有效。
const
numbers = [2, 42, 5, 304, 1, 13];
//
调用
max()
方法处理数组值以及额外三个参数
const
maximum = Math.max(24, ...numbers, 96, 7);
如果参数的顺序十分重要,你或许不想使用这种方法。因为数组很容易比你预想得
大一点或小一点,从而导致其他参数被移到新的位置,使其他参数的意义发生巨大
变化。
5.5.4
参考
5.7
节有个示例说明如何使用展开运算符合并数组。
5.15
节介绍如何使用展开运算
符删除元素。
5.6
节介绍如何使用展开运算符复制数组。
5.6
克隆数组
5.6.1
问题
你想创建一个现有数组的副本。
5.6.2
方案
使用展开运算符展开数组,把得到的元素放入新数组。
const
numbers = [2, 42, 5, 304, 1, 13];
const
numbersCopy = [...numbers];
使用
Array.slice()
方法也
不错,不要传入参数,切下整个数组。
数组
115
const
numbers = [2, 42, 5, 304, 1, 13];
const
numbersCopy = numbers.slice();
这两种方法都比自己动手遍历数组元素构建新数组要好。 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript Patterns

JavaScript Patterns

Stoyan Stefanov
JavaScript

JavaScript

David Flanagan
JavaScript

JavaScript

T. J. Crowder

Publisher Resources

ISBN: 9787519869601