Skip to Content
精通模块化JavaScript
book

精通模块化JavaScript

by Nicolás Bevacqua
April 2020
Intermediate to advanced
177 pages
1h 45m
Chinese
Publishing House of Electronics Industry
Content preview from 精通模块化JavaScript
100
4
内部构造
这种类型的操作应该设计得非常明确以防混淆,不过还是要尽可能避免这
种操作。如果认为函数就是输入与输出之间的方程式,这种不明智的想法
会带来很多负面影响。在函数体内修改输入就是其中一个例子,这样做可
能引入
bug
,令人困惑,而且难以追踪这些修改的源头。
我们经常能看到一些修改输入参数然后返回这些参数的函数。这在使用
Array#map
回调时较为常见,开发者想要修改列表中每个对象的一两个属性,
但还让原来的对象作为集合中的元素,如以下代码所示
movies.map(movie => {
movie.profit = movie.gross - movie.budget
return movie
})
在这些情况中,最好避免同时使用
Array#map
,而是使用
Array#forEach
for..of
,如下所示
for (const movie of movies) {
movie.profit = movie.gross - movie.budget
}
假设你想用类似“票房收入大于
1500
万美元”的条件过滤
movies
,但是
Array#forEach
for..of
都不支持链式调用,它们是纯粹的循环,不产
生任何输出。不过,这是一个好问题,因为它将
movie
层级的数据变化和
movies
层级的数据转换做了显式分离。在
movie
层级,我们给
movies
中的
每一项增加了一个
profit
属性,而在
movies
层级,我们希望产生一个全新
的只包含高票房电影的集合
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

数据驱动力:企业数据分析实战

数据驱动力:企业数据分析实战

Carl Anderson
基于Python的智能文本分析

基于Python的智能文本分析

Benjamin Bengfort, Rebecca Bilbro, Tony Ojeda
Lua游戏AI开发指南

Lua游戏AI开发指南

Posts & Telecom Press, David Young

Publisher Resources

ISBN: 9787121387074