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 经典实例:第三版
数组
113
JavaScript
中,剩余运算符与展开运算符的写法一样(在变量前放三个点
号)。在代码中,二者给人的感觉也是一样的,不过它们的作用却是互补的。
剩余运算符把多出的值聚在一起,全部塞进一个数组中。展开运算符则把一
个数组(或其他可迭代的对象类型)展开,变成一个个单独的值。
目前所见的代码均在一个语句中声明变量和赋值。与创建普通的变量一样,这两步
也可以分开。只是,别忘了方括号,因为这样才能表明你是在析构数组。
let
arizona, missouri, idaho, nebraska, texas, minnesota;
[arizona, missouri, idaho, nebraska, texas, minnesota] = stateValues;
5.4.4
参考
如果你想把一个数组转换成一组值,但是不赋值给变量,请使用
5.5
节介绍的展开
运算符。
5.5
把数组传给接受一组值的函数
5.5.1
问题
你想把数组中的一组值传给函数,但是该函数只接受一组值,不接受一个数组对象。
5.5.2
方案
使用展开运算符展开数组。下面使
Math.max()
方法举个例子。
const
numbers = [2, 42, 5, 304, 1, 13];
//
这个句法无效,结果为
NaN
const
maximumFail = Math.max(numbers);
//
但是借助展开运算符,可以这样写(结果为
304
const
maximum = Math.max(...numbers);
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