ES6 's modular system

Source: Internet
Author: User
Tags export class

Original address: https://hacks.mozilla.org/2015/08/es6-in-depth-modules/

ES6 is the abbreviation for the 6th edition of ECMAScript, which is the standard for a new generation of JavaScript. ES6 in Depth is an introduction to a series of new features in ES6.

ES6 is the abbreviation for the 6th edition of ECMAScript, which is the standard for a new generation of JavaScript. ES6 in Depth is an introduction to a series of new features in ES6.

Thinking back for 2007 years, when I started working in Mozilla's JavaScript team, the typical JavaScript program had only one line of code at that time.

Two years later, Google Maps was released. But not so long ago, the main use of JavaScript was form validation, and of course, your <input onchange=> processor had only one line on average.

Shina, JavaScript projects have become huge, and communities have developed tools to help develop extensible programs. The first thing you need is a modular system. The modular system allows you to spread your work across different files and directories, allowing them to access each other, and loading them very efficiently. Naturally, JavaScript developed a modular system, in fact a plurality of modular systems (Amd,commonjs,cmd, translator note). In addition, the Community provides package management tools (NPM, translator's note) that allow you to install and copy software that is highly dependent on other modules. You might think that ES6, with its modular features, came a little late.

Module Basics

A ES6 module is a file that contains the JS code. There are no keywords in ES6 module . A module looks just like a normal script file except for the following two differences:

    • The ES6 module automatically turns on strict mode, even if you do not write ‘use strict‘ .

    • You can use it in the module import andexport.

Export exports into named imports and default exports

Named Import

Let's take a look at it first export . Anything declared in the module is the default private, and if you want to public with other modules, you must have export that part of the code. We have several implementations, and the simplest way is to add a export keyword.

Kittydar.js-find the locations of all the cats in a image.//(Heather Arthur wrote this library for Real)//(But she Didn ' t use modules, because it is) export function detectcats (canvas, options) {  var Kittydar = new Kittydar (opt ions);  return kittydar.detectcats (canvas);} Export class Kittydar {  ... several methods doing image processing ...} This helper function isn ' t exported.function Resizecanvas () {  ...} ...

  File Utility.js in export form:

function Generaterandom () {    return Math.random (),} function sum (A, b) {    return a + B;} export {generaterandom, su M}//keyword Export these two functions are exported, and you can see that the two exported functions are surrounded with curly braces, and they are separated by commas. You can also rename the exported content using the following method. Export {Generaterandom as Random, sum as Dosum}

Let's take a look at how the other modules are the values of those exports.

App.js

Import {generaterandom, sum} from ' utility '; Console.log (Generaterandom ());//logs a random Numberconsole.log (sum (1, 2));//3

Note the first line. It will import values from the utility module. Of course, if you only want to export a value (for example, sum), you can use the following method:

Import {generaterandom, sum} from ' utility ';

  Similarly, you can import the entire module as an object and then refer to it using the object properties method. So we can use the following method:

Import ' utility ' as utils; Console.log (Utils.generaterandom ());//logs a random Numberconsole.log (Utils.sum (1, 2));//3

  Default Export

"Default Export" and "re-export", (default exports and re-exporting),If you only want to export a value from a module, you can use the default export. To demonstrate the use of the default export, we need to modify the Utility.js
var utils = {  generaterandom:function () {    return math.random ();     },  sum:function (A, b) {    return a + b ;  }}; Export default utils;

The last line utils this variable for export. In another module, the reference method is as follows:

App.js

Import utils from ' utility '; Console.log (Utils.generaterandom ());//logs a random Numberconsole.log (Utils.sum (1, 2));//3export default utils; Exports the imported value

The first line of code first imports the Utils object, and you can also export it again. As the last line

Default Export

Data Source:

ES6 's modular system

Understanding the modules of ES6

ES6 's modular system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.