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
26
2
模块化原则
2.1.2
 
API
优先原则
一个模块的好坏取决于它的公共接口。一个极好的接口后面可能隐藏着一
个糟糕的实现。更重要的是,一个好的接口意味着,只要我们有时间引入
一个更好的实现,就可以替换掉原先那个较差的实现。由于
API
保持不变,
我们可以决定是全部替换现有的实现,还是在使用者升级到新版本时保持
两者共存。
有缺陷的
API
很难修复。有一些实现可能依赖我们打算修改的那个接口,
这意味着只要想对
API
本身进行更改,我们就必须让每个使用者更改其调
用该
API
的方式。随着项目的发展,这些可能需要调整的
API
被调用的次
数会不断增加,因而更难被修复。
专注于公共接口的设计是开发可维护组件系统的关键。设计精良的接口可
以通过引入符合该接口的新实现来经受住时间的考验。好的接口设计可以
使访问组件的最基本或最常见的用例变得简单,而且有足够的灵活性来支
持出现的其他用例。
一个接口通常不需要支持多个实现,但我们必须优先从公共
API
的角度来
思考。对实现进行抽象只是这个难题的一小部分。
API
设计的解决之道在
于弄清楚使用者需要哪些属性和方法,同时使接口尽可能小。
当我们需要实现一个新组件时,一条很好的经验法则是 :根据这个新组件
制定我们需要的
API
调用方式。例如,我们可能希望组件与
Elasticsearch
REST API
交互。
Elasticsearch
是一个具有高级搜索和分析能力的数据库引
擎,其文档存储在索引中,并且按类型排列。
在下面这段代码中,我们想象使用一个
./elastic search
组件,该组件有 ...
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