- 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.
- Hands-On Chatbots and Conversational UI Development
- SD-WAN架構(gòu)與技術(shù)(第2版)
- 雷達(dá)饋線技術(shù)
- 圖解手機(jī)元器件維修技巧
- C/C++串口通信:典型應(yīng)用實(shí)例編程實(shí)踐
- 6G新技術(shù) 新網(wǎng)絡(luò) 新通信
- Building Web Applications with ArcGIS
- 5G技術(shù)與標(biāo)準(zhǔn)
- jQuery Mobile Web Development Essentials
- 計(jì)算機(jī)網(wǎng)絡(luò)技術(shù)及應(yīng)用
- React Cookbook
- Getting Started with nopCommerce
- 全聯(lián)網(wǎng)標(biāo)識(shí)服務(wù)
- 人際網(wǎng)絡(luò)
- 物聯(lián)網(wǎng),So Easy!