- Mastering Angular Components
- Gion Kunz
- 275字
- 2021-07-23 17:23:40
Modules
Modules provide a way to encapsulate your code and create privacy. In object-oriented languages, we usually use classes for this. However, I actually believe that this is an anti-pattern rather than a good practice. Classes should be used where inheritance is desired and not just to structure your code.
I'm sure that you've encountered a lot of different module patterns in JavaScript already. One of the most popular ones that creates privacy using a function closure of an immediately invoked function expression (IIFE) is probably the revealing module pattern. If you'd like to read more about this and maybe other great patterns, I recommend the book Learning JavaScript Design Patterns, by Addy Osmani.
Within ECMAScript 6, we can now use modules to serve this purpose. We simply create one file per module, and then we use the import and export keywords to connect our modules together.
Within the ECMAScript 6 module specification, we can actually export as many things as we like from each module. We can then import these named exports from any other module. We can have one default export per module, which is especially easy to import. Default exports don't need to be named, and we don't need to know their name when importing them:
import SomeModule from './some-module.js';
var something = SomeModule.doSomething();
export default something;
There are many combinations on how to use modules. We will discover some of these together while working on our task management application during the upcoming chapters. If you'd like to see more examples on how to use modules, I can recommend the Mozilla Developer Network documentation (https://developer.mozilla.org) on the import and export keywords.
- 黑客攻防實戰技術完全手冊:掃描、嗅探、入侵與防御
- 萬物互聯:蜂窩物聯網組網技術詳解
- 大話社交網絡
- Practical Web Design
- Wireshark網絡分析就這么簡單
- 計算機網絡與通信(第2版)
- Unity Artificial Intelligence Programming
- Echo Quick Start Guide
- 網絡空間全球治理觀察
- TCP/IP基礎(第2版)
- 從物聯到萬聯:Node.js與樹莓派萬維物聯網構建實戰
- Microservices Development Cookbook
- 一本書讀懂移動物聯網
- 通信系統實戰筆記:無處不在的信號處理
- Learning IoT with Particle Photon and Electron