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
2.1
 模块化设计的要点
21
何帮助我们在组件级别和规模上管理复杂性呢?
2.1
 模块化设计的要点
通过采用有详细的文档记录、有清晰的划分且经过充分测试的
API
的小模
块,模块化设计解决了程序设计中的复杂性问题。定义清晰的
API
会降低
模块间互相连接时的复杂性,而小模块则使程序更易于理解和使用。
2.1.1
 单一职责原则
单一职责原则(
Single Responsibility Principle
SRP
)可能是公认的最成
功的应用程序模块化设计原则。当组件有一个唯一的精确目标时,就称它
们遵循
SRP
遵循
SRP
的模块不一定必须导出一个函数作为该模块的
API
。只要从组件
中导出的方法和属性是相互关联的,我们就不会破坏
SRP
在考虑
SRP
时,很重要的一点是要弄清楚其中的“
R
(职责)”是什么。例如,
我们有一个组件,用于通过简单邮件传输协议(
SMTP
)发送邮件。选择使
SMTP
发送邮件可以看作是实现细节。如果我们以后想使用模板和模型
HTML
页面呈现在这些邮件中,这是否也属于邮件发送者的职责?
假设我们在同一个组件中开发了发送邮件和内容模板的功能,此时它们是
紧耦合的。此外,如果我们以后想要通过
API
的方式,从
SMTP
切换到事
务性邮件提供商所提供的解决方案,就必须小心,不要干扰位于同一模块
中的模板功能。
22
2
模块化原则
以下代码片段表示一段紧耦合的代码,它混杂了模板化、格式化、邮件
API
客户端实例化和邮件发送等功能
import insane from 'insane'
import mailApi ...
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