Skip to content

Commit b014b46

Browse files
committed
Blog: Update post.
1 parent c201053 commit b014b46

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

book/programming-javascript-applications/chapter-5/500-modules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ When you design part of a system to a modular interface contract, you can safely
2121

2222
Another important goal of modularity is the ability to reuse your module in other applications. Well designed modules built on similar frameworks should be easy to transplant into new applications with few (if any) changes. By defining a standard interface for application extensions, and then building your functionality on top of that interface, you'll go a long way toward building an application that is easy to extend and maintain, and easy to reassemble into different forms in the future.
2323

24-
模块化的另一个重要目标是令你的模块在其它应用程序中得到重用。模块只要基于相似的框架进行良好的设计,就可以很容易地移植到新的应用程序中,而几乎不需要(即使需要也很少)修改。我们可以为应用程序的扩展性定义一个标准接口,然后在这个接口之上构建功能。这对于构建一个易于扩展和维护的应用程序来说大有裨益,这样的应用程序也易于在未来重新组装为不同的形态。
24+
模块化的另一个重要目标是令模块在其它应用程序中得到重用。模块只要基于相似的框架进行良好的设计,就可以很容易地移植到新的应用程序中,而几乎不需要(即使需要也很少)修改。在对应用程序进行扩展的时候,我们可以先为扩展部分定义一套标准接口,然后在这个接口之上构建新功能——这种方式对于构建一个易于扩展和维护的应用程序来说大有裨益,这样的应用程序也易于在未来重新组装为不同的形态。
2525

2626
JavaScript modules are encapsulated, meaning that they keep implementation details private, and expose a public API. That way, you can change how a module behaves under the hood without changing code that relies on it. Encapsulation also provides _protection_, meaning that it prevents outside code from interfering with the functionality of the module.
2727

28-
JavaScript 模块是封装式的,这意味着它们将实现细节保留在内部,并且向外暴露公开的 API。这样,你可以随时改变一个模块的内部行为,而不需要同时去修改那些依赖它的外部代码。封装同样提供了一种“保护”,这意味着它会防止外部代码干涉模块内部的功能。
28+
JavaScript 模块是封装式的,这意味着它们将实现细节保留在内部,仅向外暴露一套公开 API。这样一来,你就可以随时改变一个模块的内部行为,而不需要同时去修改那些依赖它的外部代码。封装同样提供了一种“保护”,这意味着它会防止外部代码干涉模块内部的功能。
2929

3030
There are several ways to define modules in JavaScript. The most popular and common are the module pattern, CommonJS modules (used in Node), and AMD (Asynchronous Module Definition).
3131

book/programming-javascript-applications/chapter-5/501-principles-of-modularity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Modules should be:
3939
4040
> _The Open Closed Principle_ states that a module interface should be open to extension but closed to modification. Changing an interface that a lot of software relies on can be a daunting task. It's best if you can avoid making changes to an existing interface once it has been established. However, software should evolve, and as it does, it should be easy to extend existing interfaces with new functionality.
4141
42-
> **开闭原则** 的意思是,一个模块的接口应该对于扩展是开放的,而对于修改是关闭的(禁止修改)。修改一个很多软件都依赖的接口往往是一项令人望而却步的任务。避免对一个既有接口做修改,是上上策。不过,软件产品应该(也一直在)不断进化,因此,为既有接口扩展新功能不应该成为一件难事。
42+
> **开闭原则** 的意思是,一个模块的接口应该对于扩展是开放的,而对于修改是关闭的(译注:即禁止修改)。修改一个很多软件都依赖的接口往往是一项令人望而却步的任务。避免对一个既有接口做修改,是上上策。不过,软件产品应该(也一直在)不断进化,因此,为既有接口扩展新功能不应该成为一件难事。
4343
4444
***
4545

book/programming-javascript-applications/chapter-5/502-interfaces.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ Figure 5-2. Interface
4141

4242
Other languages have native support for interfaces which may enforce the requirements of an interface. You might know them as abstract base classes, or pure virtual functions. In JavaScript, there is no distinction between a class, interface, or object instance. There are only object instances, and that simplification is a good thing. You may be wondering, if there's no native support for interfaces in JavaScript, why bother to write one at all?
4343

44-
其它语言对接口特性提供了原生支持——可以指定一个接口的依赖条件。你可能知道这种支持称作抽象基类,或纯虚函数。在 JavaScript 中,类、接口和对象实例之间并没有什么区别。它们都只是对象实例,而且这种简化也有它的好处。你可能会问,既然 JavaScript 没有对接口特性提供原生支持,我们又何必要写接口呢
44+
其它语言对接口特性提供了原生支持,比如可以指定接口需求。你可能知道这种支持称作“抽象基类”或“纯虚函数”。在 JavaScript 中,类、接口和对象实例之间并没有什么区别。它们都只是对象实例,而且这种简化也有它的好处。你可能会问,既然 JavaScript 没有对接口特性提供原生支持,我们又何必要自己写一个出来呢
4545

4646
When you need multiple implementations of the same interface, it's good to have a canonical reference in the code that explicitly spells out exactly what that interface is. It's important to write code that is self-documenting. When it's time to add another concrete instance, simply call a predefined factory function and pass in the methods you need to override.
4747

48-
当你需要为相同的接口编写多个实现时,在代码中使用规范的文档格式来清晰地描述出接口的用途,是一种良好的实践。编写可以自我描述的代码是很重要的。当你需要创建一个新的具体实例时,只需要简单地调用一个预定义的工厂函数并传入你需要覆盖的方法就可以了。(译法:本节提到的“实现”指的是对接口的实现,我们可以为相同的接口编写多个不同的实现。)
48+
当你需要为相同的接口编写多个实现时,在代码中使用规范的文档格式来清晰地描述出接口的用途,是一种良好的实践。编写可以自我描述的代码是很重要的。当你需要创建一个新的具体实例时,只需要简单地调用一个预定义的工厂函数并传入你需要覆盖的方法就可以了。
4949

5050
For example, using `O.js` to define the factory:
5151

0 commit comments

Comments
 (0)