- Learning Node.js for .NET Developers
- Harry Cummings
- 512字
- 2021-07-02 16:29:06
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.
- Spring Boot 2實戰之旅
- 演進式架構(原書第2版)
- 零基礎搭建量化投資系統:以Python為工具
- 編程的修煉
- PowerCLI Cookbook
- NLTK基礎教程:用NLTK和Python庫構建機器學習應用
- Wireshark Network Security
- Hands-On C++ Game Animation Programming
- ADI DSP應用技術集錦
- Instant Nancy Web Development
- Java系統化項目開發教程
- AIRIOT物聯網平臺開發框架應用與實戰
- Java Web開發就該這樣學
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- Java 從入門到項目實踐(超值版)