官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 酉阳| 监利县| 额敏县| 南康市| 鹤壁市| 滕州市| 东山县| 南丰县| 灌南县| 专栏| 吉首市| 泌阳县| 万全县| 苗栗市| 民丰县| 和龙市| 华蓥市| 巍山| 宣城市| 通渭县| 茌平县| 上林县| 临颍县| 海伦市| 隆尧县| 花莲县| 道孚县| 察雅县| 苏州市| 南川市| 丘北县| 宿州市| 碌曲县| 博爱县| 浦北县| 平昌县| 桓仁| 新竹县| 海盐县| 东阿县| 溧阳市|