项目作者: mayeedwin

项目描述 :
practical es6 javascript modules hands on guide
高级语言: HTML
项目地址: git://github.com/mayeedwin/es6-modules.git
创建时间: 2019-12-09T17:17:52Z
项目社区:https://github.com/mayeedwin/es6-modules

开源协议:Apache License 2.0

下载


JavaScript ES6 Modules 🎒

Author : Maye Edwin

Modules in javascript allow you to split JavaScript programs up into separate chuncks that can be imported when needed.

Modern browsers now support module functionality natively - browsers can optimize loading of modules,
making it more efficient than having to use a library to achieve the same.

Why modules?

  1. Maintainability

  2. Resusability

  3. Perfomance

  4. Productivity

Read more about JavaScript Modules.

Using Modules ( Export and Import )

export : is used to expose public variables, functions and classes.

  1. // Add.js
  2. const Add = () {
  3. return a + b;
  4. }
  5. export { Add }

import : is then used to pull items from a module into another script or module.

  1. // App.js
  2. import { Sum } from "./Add.js";
  3. console.log(Add(1, 2)); // 3

Advanced Recipe

A single export statement can be used as shown below.

  1. export { Add, Subtract };

A single import statement can be used as shown below for all public items.

  1. import { Add, Subtract } from "./Calculate.js";

Or use export as you declare a function, class or variable.

  1. // export a function
  2. export const Add = () {
  3. return a + b ;
  4. }
  5. // or a variable
  6. export let name = `Maye Edwin`;

Or use as and * to import all exports from a given module.

  1. import * as App from "./Calculate.js";
  2. // using each export
  3. App.Add();
  4. App.Divide();

Or use as to import an export or exports from a given module (helps to prevent naming problems).

  1. import { user as Bio } from "./Users.js";
  2. import { sum as Add, product as Multiply } from "./Calculator.js";

Using ES6 Modules

All module import scripts must be loaded by setting a type=”module” attribute in the