- Building Enterprise JavaScript Applications
- Daniel Li
- 97字
- 2021-07-23 16:31:08
Fulfilling the encapsulation requirement
You can export multiple constructs from a single module by adding them as properties to the exports object. Constructs that are not exported are not available outside the module because Node.js wraps its modules inside a module wrapper, which is simply a function that contains the module code:
(function(exports, require, module, __filename, __dirname) {
// Module code
});
This fulfills the encapsulation requirement of modules; in other words, the module restricts direct access to certain properties and methods of the module. Note that this is a feature of Node.js, not CommonJS.