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
86
4
内部构造
}
// ...
最好把这样的赋值重构到一个函数中,就像下面的代码那样。需要注意的
是,这个函数有一个
user
参数,所以可以把该函数从我们原来定义这个
user
对象的作用域链中移出,同时把
let
绑定改为
const
绑定。当继续往
下阅读代码时,使用
const
的好处就是我们知道该绑定不会改变。如果使
let
,就无法确定随着时间的推移该绑定是否会发生变化,也意味着读者
理解算法时要注意的一堆事情中又增加了一件
// ...
const website = getUserWebsite(user)
// ...
function getUserWebsite(user) {
if (user.details) {
return user.details.website
}
if (user.website) {
return user.website
}
return null
}
不管你喜欢采用什么样的变量绑定,最好将选择应用状态的那部分代码和
根据所选状态执行操作的逻辑分离开来。采用这种方式,我们就不会在如
何选择状态这件事情上分心,而是专注于应用程序的逻辑试图执行的操作。
如果你希望为程序的某个部分命名而且不添加任何注释,可以创建一个函
数来包含这个功能。这样做不仅为算法所做的事情取了名字,而且使我们
能将这段代码移出来,仅保留对流程的高层描述。
4.2
 重构复杂代码
87
4.2.5
 扁平化嵌套回调
具有异步流程的代码库经常陷入回调地狱,每个回调创建新一级的缩进 ...
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