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

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.

主站蜘蛛池模板: 内黄县| 光山县| 江油市| 肥城市| 德令哈市| 长岛县| 威远县| 新晃| 开原市| 武城县| 什邡市| 都兰县| 萨嘎县| 延安市| 堆龙德庆县| 浮梁县| 揭东县| 阿勒泰市| 高州市| 原平市| 太湖县| 武陟县| 固始县| 永丰县| 呼伦贝尔市| 盐亭县| 喜德县| 南川市| 南丹县| 仁化县| 邵东县| 东乌珠穆沁旗| 连江县| 察隅县| 晋江市| 历史| 晋宁县| 东方市| 唐海县| 鹤峰县| 乌鲁木齐市|