Skip to Content
学习 JavaScript 设计模式,第二版
book

学习 JavaScript 设计模式,第二版

by Addy Osmani
May 2025
Intermediate to advanced
298 pages
3h 29m
Chinese
O'Reilly Media, Inc.
Content preview from 学习 JavaScript 设计模式,第二版

第 5 章 现代 JavaScript 语法和功能 现代 JavaScript 语法和功能

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

JavaScript 已有几十年的历史 ,并经历了多次修订。本书探讨现代 JavaScript 环境下的设计模式,并在所有讨论的示例中使用现代 ES2015+ 语法。本章将讨论 ES2015+ JavaScript 的特性和语法,这些特性和语法对于我们进一步讨论当前 JavaScript 环境下的设计模式至关重要。

备注

ES2015 对 JavaScript 语法进行了一些基本修改,这些修改与我们关于模式的讨论尤为相关。BabelJS ES2015 指南对此进行了详细介绍。

本书依赖于现代 JavaScript 语法。您可能还会对 TypeScript 感到好奇。TypeScript是JavaScript的静态类型超集,它提供了JavaScript所没有的几种语言特性。这些特性包括强类型、接口、枚举和高级类型推断,还可以影响设计模式。要了解有关 TypeScript 及其优势的更多信息,可以考虑查看一些 O'Reilly 的书籍,如 Boris Cherny 所著的《Programming TypeScript》。

应用程序解耦的重要性

模块化 JavaScript 允许您从逻辑上 将应用程序分割成称为模块的小块。一个模块可以被其他模块导入,而其他模块又可以被更多模块导入。因此,应用程序可以由许多嵌套模块组成。

在可扩展 JavaScript 的世界里, ,当我们说一个应用程序是模块化的时候,我们通常是指它是由存储在模块中的一系列高度解耦的、不同的功能片段组成的。松耦合通过尽可能消除依赖关系,使应用程序更易于维护。如果实施得当,它还能让您了解系统中某一部分的变化会对另一部分产生怎样的影响。

与一些更传统的编程语言不同, ,在 ES5(标准 ECMA-262 5.1 版)之前,JavaScript 的旧版迭代并没有为开发人员提供干净利落地组织和导入代码模块的手段。这也是规范中需要考虑的问题之一,直到最近几年,人们才发现需要更有条理的 JavaScript 应用程序。AMD(异步模块定义)CommonJS模块是 JavaScript 最初版本中最流行的解耦应用程序模式。

解决这些问题的原生解决方案 随着ES6 或 ES2015 的问世而到来。TC39 是负责定义 ECMAScript 及其未来迭代版本的语法和语义的标准机构,它一直密切关注 JavaScript 在大规模开发中的使用演变,并敏锐地意识到需要更好的语言特性来编写更模块化的 JS。

随着 ES2015 中 ECMAScript 模块的发布,JavaScript 中创建模块的语法得到了发展和规范。如今,所有主流浏览器都支持 JavaScript 模块。模块已成为 JavaScript 中实现现代模块化编程的事实方法。在本节中,我们将探索使用 ES2015+ 中模块语法的代码示例。

带进出口的模块

模块允许我们将应用程序 代码分离成独立的单元,每个单元包含一个方面功能的代码。模块还能提高代码的可重用性,并提供可集成到不同应用程序中的功能。

一门语言应具备以下功能:允许您import 模块依赖关系和export 模块接口(我们允许其他模块使用的公共 API/变量),以支持模块化编程。ES2015 中为 JavaScript 引入了对 JavaScript模块(也称为 ES ...

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

图解大模型 : 生成式AI 原理与实战

图解大模型 : 生成式AI 原理与实战

Jay Alammar, Maarten Grootendorst
学习 MySQL,第二版

学习 MySQL,第二版

Vinicius M. Grippa, Sergey Kuzmichev

Publisher Resources

ISBN: 9798341657861