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
 模块化设计的要点
31
2.1.4
 寻找正确的抽象
开源软件组件通常会收到不少特性请求(
feature request
)。这些请求对某个
特定用户的需求来说过于具体,仅仅从表面上考虑特性请求或需求是不够
;相反,我们需要深入挖掘,并且在该需求的特性、我们为路线图规划
的特性,以及我们可能希望在未来调整组件以支持的特性之间找到共同点。
诚然,对于一个组件来说,满足大多数使用者的需求是很重要的,但这并
不意味着我们应该试着一个一个地或孤立地满足用例中的需求。毫无疑问,
这样做的结果就是 :逻辑重复、在
API
层级上不一致,以及实现同一目标
有好几种方式,而这个目标通常与观察到的结果不一致。
如果能发现一个共同点,抽象时涉及的冲突较少,则有助于避免前面提到
的各种不一致之处。例如,考虑
DOM
事件监听器的例子 :对于每一个事件
处理程序,我们都有一个
HTML
属性和与之匹配的
JavaScript DOM
元素属
性,例如
onlick
onchange
oninput
等。可以为每个属性分配一个处理事
件的
listener
函数。然后是
EventTarget#addEventListener
方法,它有一
个类似
addEventListener(type, listener, options)
1
的签名,将所有事件
处理逻辑集中到一个以事件类型(
type
)作为参数的方法中。当然,这个
API
更好,是因为下面的几个原因。首先,
EventTarget#addEventListener
是一个方法,明确定义了它的行为。相比之下, ...
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