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.2
 
CRUST
原则:一致、弹性、明确、简单和小巧
35
部件更少。当我们消除公共接口中不纯的部分时,就能有效地限制调用代
码的熵。使用者每次都会收到一个全新的计数器,该计数器完全负责管理
自己的状态。它仍然可以将
counter
传递给其依赖方,但它可以控制依赖
方操作该状态的方式(如果有的话)。
我们在一些流行的库中观察到了这一点,比如
Node.js
中的
request
包。这
些库可以用来发送
HTTP
请求
1
request
函数在很大程度上依赖于传递给它
options
参数的合法默认值。有时我们希望使用一组不同的默认值来发
送请求。
这个库可能已经提供了一种解决方案,使我们能够在每次调用
request
更改
options
的默认值。这将是个糟糕的设计,因为它会使对
options
的处
理更加不稳定。我们可能不得不考虑代码库的方方面面,才会对最终调用
request
时使用的
options
放心。
request
选择的解决方案是使用
request.defaults(options)
方法,返回与
request
相同的
API
,但是它在现有默认值的基础上应用新的默认值。这种
方法避免了意外情况,因为修改后的
request
仅限于在调用它的代码及其
依赖项中使用。
2.2
 
CRUST
原则 :一致、弹性、明确、简
单和小巧
一个
API
要得到使用者的认可通常应该具备以下几个特点。它是
一致
consistent
),意味着它是幂等的
2
,并且具有与相关的函数相似的签名形态。
1
 你可以在
GitHub
上找到
request
2
 对于给定的一组输入,幂等函数总是产生相同的输出。 ...
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

Publisher Resources

ISBN: 9787121387074