JS Module Summary

I. Why we need to use the Module?

In the past, Javascript can execute independently, because it's small. In recent years, It's getting more and more complicated. So, we have to find a method to solve a problem that is how to execute scripts effectively. Therefore, a concept accurs - Module. According to our needs, we can export and import revent modules. There are many ways about module, this article I just talk about the export/import in ES6.

II. How to use the Module?

  • Named Export and Import
  • Default Export and Import
  • Rename Export and Import

II.I The folder structure: Named Export and Import

  • js-module

    • js

      • basic.js
      • index.js
    • index.html
// basic.js
const person = {
name: 'Vera',
age: 24,
gender: 'female'
} export {person}
// index.js
import {person} from './basic.js' console.log(person)
// index.html
<script type="module" src="./js/index.js"></script>

Tips:

  1. Please don't run code locally, eg.file:///F:/grocery-store/js-module/index.html. Because of the module's safety and CORS, if we want to run these codes , We must use a serve.
  2. We must add "type = 'module'" to the script tag to declare this script is a module as a top level module. If we don't, there are some errors. For example, Uncaught SyntaxError: Cannot use import statement outside a module.

II.II The folder structure: Default Export and Import

  • js-module

    • js

      • basic.js
      • index.js
    • index.html
// basic.js

// About basic information
const basicInfor = {
name: 'chenchen',
age: 24,
gender: 'female'
}
// About user's hobby1
const hobby1 = 'sleep sleep sleep'
// About user's hobby2
const hobby2 = 'eat eat eat'
const dosomething = (something) => 'I like' + something + '.' export default dosomething
export { basicInfor, hobby1, hobby2 }
// index.js

// import { basicInfor, hobby1, hobby2 }, dosomething  from './basic.js';   Syntax Error
import dosomething, {basicInfor, hobby1, hobby2} from './basic.js' console.log(basicInfor);
console.log(hobby1);
console.log(hobby2);
console.log(dosomething('吃饭睡觉打豆豆'));

Tips:

  1. In a module file, export default only appear once, but export can appear many times.
  2. When we use export default, we don't need braces. On the contrary, export must be bracketed. Similarly, the usage of import is same to export, but there is one thing to note that is import {default as name} from './basic.js can be abbreviated to import name from './basic.js.
  3. export const name = ... is corret. Instead, export default const name = ... is wrong.
  4. Last but not least, in a module file, the value export from export name can be changed, but the other value export from export default name cannot be changed.

II.III Rename Export and Import(How to avoid naming conflicts?)

Please wait patiently....

JS Module的更多相关文章

  1. node.js module初步理解

    在开发一个复杂的应用程序的时候,我们需要把各个功能拆分.封装到不同的文件,在需要的时候引用该文件.没人会写一个几万行代码的文件,这样在可读性.复用性和维护性上都很差,几乎所有的编程语言都有自己的模块组 ...

  2. 如何發佈一個完整Node.js Module

    本文會透過以下幾個段落,讓各位一步一步學習如何寫一個自已的Node.js Module並且發佈到npm package上 Node.js Module 結構 我們先建立一個 NodeModuleDem ...

  3. (转)Node.js module.exports与exports

    本文转自Node.js module.exports与exports 作者: chemdemo 折腾Node.js有些日子了,下面将陆陆续续记录下使用Node.js的一些细节. 熟悉Node.js的童 ...

  4. 创建并发布node.js module

      创建node.js module. 创建一个文件夹,用来存放module. Cd到新创建的文件夹,运行npm init,会提示输入package的信息. 可以按照这个视频的来输入.Test com ...

  5. Node.js & module system

    Node.js & module system Node.js v10.9.0 Documentation https://nodejs.org/api/modules.html#module ...

  6. node.js module.exports & exports & module.export all in one

    node.js module.exports & exports & module.export all in one cjs const log = console.log; log ...

  7. Node.js & module.exports & exports

    Node.js & module.exports & exports https://www.cnblogs.com/xgqfrms/p/9493550.html exports &a ...

  8. node.js module初步理解-(转载)

    在开发一个复杂的应用程序的时候,我们需要把各个功能拆分.封装到不同的文件,在需要的时候引用该文件.没人会写一个几万行代码的文件,这样在可读性.复用性和维护性上都很差,几乎所有的编程语言都有自己的模块组 ...

  9. Node.js module.exports和exports的区别

    require 用来加载代码,而 exports 和 module.exports 则用来导出代码,从接触node.js就不会它们两陌生,上代码: foo.js exports.a = functio ...

随机推荐

  1. __metaclass__方法

    metaclass这个属性叫做元类,它是用来表示这个类是由谁来帮他实例化创建的,说白了,就是相当于自己定制一个类. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  2. 【面试向】2019 年微软秋招笔试 Problem 3

    Problem Check if a positive integer $n$ can be written as sum of a positive integer and reverse of t ...

  3. DLL的创建与使用

    一.动态链接库(DLL) 动态链接库提供了一种方法,使进程可以调用不属于其执行代码的函数.函数的可执行代码位于一个.dll文件中,该文件包含一个或多个已被编译.链接并使用它们的进程分开存储的函数. 优 ...

  4. MY TESTS

    励志整理所有的n次考试的博客: [五一qbxt]test1 [五一qbxt]test2 [校内test]桶哥的问题 [6.10校内test] noip模拟 6.12校内test [6.12校内test ...

  5. Django进阶(一)

    目录 choice参数 MTV与MVC模型 Ajax Ajax传json数据 Ajax传file数据 contentType前后端传输数据编码 ajax + sweetalert 序列化组件 补充 c ...

  6. JS常见面试题总结-真实被问到的!

    1.判断数据类型有几种方法 console.log(typeof 'abc') // string console.log(Object.prototype.toString.call('abc')) ...

  7. Django框架——进阶之AJAX

    <script>$("#b1").on("click", function () { // 点击 id是b1的按钮要做的事儿 var i1 = $( ...

  8. int 问号的使用

    单问号---为泛型 Nullable<int> 的简写方式. 双问号---用于判断前一个操作数是否为null,如为null则"返回"后一个操作数,否则"返回& ...

  9. Hyperledger Fabric 环境搭建(2)

    上一篇https://www.cnblogs.com/xdyixia/p/11738096.html 介绍了Hyperledger Fabric环境中各种软件安装和源码编译,这一篇介绍快速运行一个简单 ...

  10. Delphi MaskEdit 组件