You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/programming-javascript-applications/chapter-5/503-the-module-pattern.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -9,15 +9,15 @@
9
9
10
10
Modules in the browser use a wrapping function to encapsulate private data in a closure (for example, with an IIFE, see the section an Immediately Invoked Function Expressions). Without the encapsulated function scope provided by the IIFE, other scripts could try to use the same variable and function names, which could cause a lot of unexpected behavior.
Most libraries, such as jQuery and Underscore, are encapsulated in modules.
15
15
16
-
大多数类库,比如 jQuery 或 Underscore,都封装在模块中。
16
+
大多数类库,比如 jQuery 或 Underscore,都封装在这样的模块中。
17
17
18
18
The module pattern encapsulates module contents in an immediately invoked function expression (IIFE), and exposes a public interface by assignment. Douglas Crockford gave the module pattern its name, and Eric Miraglia popularized it in a [well-known blog post on the YUI Blog][10].
The original module pattern assigns the result of the IIFE to a predefined namespace variable:
23
23
@@ -41,11 +41,11 @@ test('Module pattern', function () {
41
41
42
42
The problem with this pattern is that you have no choice but to expose at least one global variable for each module. If you're building an application with a lot of modules, that is not a good option. Instead, it's possible to pass in an existing variable to extend with your new module.
Here that variable is called `exports`, for compatibility with CommonJS (see the section on Node Modules for an explanation of CommonJS). If `exports` does not exist, you can fall back on window:
@@ -66,11 +66,11 @@ test('Pass in exports.', function () {
66
66
67
67
A common mistake is to pass in a specific application namespace inside your module's source file (instead of using a globally defined exports). Normally, that will not harm anything. However, if you wish to reuse the module in another application, you'll have to modify the source of the module in order to attach it to the correct namespace.
Instead, you can pass your application object in as exports. It's common in client side code to have a build step that wraps all of your modules together in a single outer function. If you pass your application object into that wrapper function as a parameter called `exports`, you're in business.
@@ -97,7 +97,7 @@ test('Pass app as exports.', function () {
97
97
98
98
An upside to this version of the module pattern is that the code you write with it can be easily run and tested in Node. From this point on, when the module pattern gets mentioned, this is the version that should spring to mind. It's older ancestor is obsolete.
0 commit comments