官术网_书友最值得收藏!

Organizing your codebase

Most programming platforms provide several mechanisms for structuring your code. Consider C#/.NET or Java: you can use classes, namespaces or packages, and compilation units (assemblies or JAR/WAR files). Notice the range from small-scale organizational units (classes) to large-scale ones (assemblies). This allows you to make a codebase more approachable by providing order at each level of detail.

Classic browser-based JavaScript development was quite unstructured. Functions were the only built-in language feature for organizing your code. You could split your code into separate script files, but these all share the same global context within a web page.

Over time, people have developed ways of organizing JavaScript code. The standard approach now is to use modules. There are a few different module systems available for JavaScript, but they all work in a similar way. Each module system includes the following aspects:

  • A way of declaring a module with a name and its own scope
  • A way of defining functionality provided by the module
  • A way of importing a module into another script

In each system, when you import a module, you get a plain JavaScript object that you can assign to a variable. For most modules, this will be an object with several properties containing functions. But it could be any valid JavaScript object, for example, a single function.

Most module systems expect or at least encourage you to define each module in a separate file, just as you would with classes in other languages. It is also common for large modules to be composed of other, smaller, modules. These would be grouped together under the same directory. In this way, modules act more like namespaces or packages.

The flexibility of modules means that you can use them to structure your code at different scales. The lack of a built-in hierarchy of organizational units in JavaScript provides more flexibility. It also forces you to think more about how you structure your code.

JavaScript module systems

ECMAScript 2015 introduces modules as a built-in feature of the language. They have been common practice for a while, though. For client-side programming, this practice has relied on using third-party libraries to provide a module system.

You may have seen RequireJS, which provides a way of using functions to define modules. RequireJS uses plain JavaScript and works in any environment. It is most useful in the browser, where additional modules may be loaded over the Internet. RequireJS addresses some of the pitfalls of loading additional scripts dynamically and asynchronously.

The Node.js environment has its own module system, which we will look at in the rest of this chapter. It makes use of the filesystem for organizing modules.

Tip

You might come across the terms AMD or CommonJS. These are standards for defining modules. RequireJS is an implementation of AMD, and Node.js modules follow the CommonJS standard. ECMAScript 2015 modules define a new standard with new export and import language keywords. The syntax is quite similar, though, to the Node.js module system we'll be using in this book, and it is easy to switch between the two.

主站蜘蛛池模板: 江油市| 宁国市| 泽普县| 石首市| 榆树市| 榆林市| 东源县| 丰县| 堆龙德庆县| 澜沧| 嵩明县| 珲春市| 辽源市| 西和县| 苏州市| 临桂县| 尉犁县| 萝北县| 南昌市| 泰和县| 汤原县| 安乡县| 呼图壁县| 固镇县| 双桥区| 五指山市| 石嘴山市| 清水河县| 怀宁县| 巴林左旗| 江西省| 揭阳市| 乌鲁木齐县| 白水县| 弥勒县| 赞皇县| 江华| 吴桥县| 景宁| 古蔺县| 公主岭市|