- Vue.js 2 Design Patterns and Best Practices
- Paul Halliday
- 191字
- 2021-06-24 18:33:04
JavaScript modules
In order to create reusable modular code, our aim should be to have one file per feature in most cases. This allows us to avoid the dreaded "Spaghetti code" anti-pattern, where we have strong coupling and little separation of concerns. Continuing with the pasta-oriented theme, the solution to this is to embrace the "Ravioli code" pattern with smaller, loosely coupled, distributed modules that are easier to work with. What does a JavaScript module look like?
In ECMAScript2015, a module is simply a file that uses the export keyword, and allows other modules to then import that piece of functionality:
// my-module.js
export default function add(x, y) {
return x + y
}
We could then import add from another module:
// my-other-module.js
import { add } from './my-other-module'
add(1, 2) // 3
As browsers haven't fully caught up with module imports yet, we often use tools to assist with the bundling process. Common projects in this area are Babel, Bublé, Webpack, and Browserify. When we create a new project with the Webpack template, it uses the Vue-loader to transform our Vue components into a standard JavaScript module.
- FreeSWITCH 1.2
- Modern JavaScript Web Development Cookbook
- 智能網聯汽車V2X與智能網聯設施I2X
- 物聯網檢驗檢測技術
- 物聯網概論(第2版)
- JBoss EAP6 High Availability
- 計算機網絡安全實訓教程(第二版)
- Socket.IO Real-time Web Application Development
- 數字調制解調技術的MATLAB與FPGA實現:Altera/Verilog版(第2版)
- Getting Started with Memcached
- 網絡安全之道
- 黑客與反黑工具使用詳解
- 互聯網戰略變革與未來
- 互聯網安全的40個智慧洞見(2018)
- Hands-On Cloud:Native Microservices with Jakarta EE