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
5.3
 代码模式
127
量使用其他模式相比,前者会更难以理解、使用和构建。为每种情况选择
正确的模式与努力保持一致性,看上去有点矛盾,但这不过是一种权衡,
权衡代码库中的大规模一致性与我们正在处理的本地代码片段的简单性。
那么问题来了:我们是否获得了足够的简单性以至于需要牺牲一些一致性?
5.3
 代码模式
接下来,我们更深入地分析架构设计方面的具体内容。本节我们将探讨一
些最常见的设计模式,使用这些模式可以有效减少代码复杂性、封装功能
以及完成应用层级的通信。
5.3.1
 揭示模块(
revealing module
揭示模块模式已成为
JavaScript
世界的一个主要模式。使用这种模式的前
提很简单 :准确暴露使用者能够访问的内容,并避免暴露其他内容。之所
以这样做,原因是多方面的。防止对实现细节的未授权访问,降低了模块
接口被滥用的可能性,因为不被支持的用例可能会给模块开发者和使用者
带来麻烦。
私有方法必须避免被暴露。例如,假设有一个
_calculatePriceHistory
方法,
它用下画线前缀作为阻止直接访问的一种方式,并表明其应被视为私有的。
要避免使用这些方法,以防止测试代码直接访问私有方法,导致测试只针
对接口进行断言,并且稍后这些测试还可以作为如何使用接口的文档被引
用。这种方法还防止使用者动态扩展内部实现细节,从而使接口变得更加
清晰易懂。最后,这种方法产生的接口通常会更整洁,因为接口就是一切,
你找不到其他途径可以直接使用模块的内部组件与模块交互。
128
5
模块化模式与实践
JavaScript ...
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