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
8
1
模块化思维
用这些函数的方法,使
RequireJS
的使用变得更加复杂。
API
并不是最直观
的,因为有很多方法可以做这件事情——用依赖项来声明模块。
AngularJS
的依赖注入(
dependency injection
)系统也有许多同样的问题。
它在当时是一种优雅的解决方案,依靠巧妙的字符串解析来避免依赖数组,
并改用函数参数名来解决依赖关系。这种机制与代码压缩工具(
minifier
不兼容,代码压缩工具会将参数重命名为单个字符而破坏依赖注入机制。
后来,
AngularJS v1
版本引入了一个构建任务,将如下代码
module.factory('calculator', function(mathlib) {
// ...
})
转换为下面的格式,因为它加入了明确的依赖项列表,所以可以放心地压缩:
module.factory('calculator', ['mathlib', function(mathlib) {
// ...
}])
因为
AngularJS
引入这个鲜为人知的构建工具的时机太晚,而且它需要额
外的构建步骤来恢复不该被破坏的东西,有过度设计之嫌,而带来的好处
小到可以忽略不计,这些因素都阻碍了该模式的使用。因此,开发者大多
会坚持用他们熟悉的类似
RequireJS
那种写死依赖关系的数组格式。
1.2.3
 
Node.js
CommonJS
的出现
谈到
Node.js
衍生的若干创新,
CommonJS
模块系统算得上一个,它被简称为
CJS
。由于利用了
Node.js
程序访问文件系统的能力,因此 ...
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