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
40
2
模块化原则
$('{div}')
// <- Uncaught Error: unrecognized expression: {div}
除了重载之外,
jQuery
还提供了大量可选参数。虽然重载就是以不同的方
式接受一种特定的输入,但可选参数有不同的用途,其中之一就是增加一
个函数以支持更多用例。
关于可选参数有一个很好的例子,就是原生的
DOM
fetch
API
。在下面的
代码片段中,有两个
fetch
调用。第一个只接收我们想要获取的
HTTP
源的字符串,假设所使用的是
GET
方法。在第二个示例中,我们指定了第
二个参数,并指出我们想使用
HTTP
DETELE
方法
await fetch('/api/users')
await fetch('/api/users/rob', {
method: 'DELETE'
})
假设作为
fetch
API
设计人员,我们最初只是将它设计为一种
GET
${resource}
的方法。当我们需要选择
HTTP
方法时,可以避免使用
options
对象,并直接访问
fetch(resource, verb)
重载。虽然满足了我们
的特殊需求,但这是一种目光短浅的做法。一旦收到要配置其他东西的需求,
我们就需要同时支持
fetch(resource, verb)
fetch(resource, options)
重载,这样才能避免破坏向后兼容性。更糟糕的是,我们可能会引入第三
个参数来配置下一个需求。很快,我们就会得到一个
API
,比如臭名昭著
KeyboardEvent#initKeyEvent ...
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