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
  1. // basic.js
  2. const person = {
  3. name: 'Vera',
  4. age: 24,
  5. gender: 'female'
  6. }
  7. export {person}
  1. // index.js
  2. import {person} from './basic.js'
  3. console.log(person)
  1. // index.html
  2. <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
  1. // basic.js
  2. // About basic information
  3. const basicInfor = {
  4. name: 'chenchen',
  5. age: 24,
  6. gender: 'female'
  7. }
  8. // About user's hobby1
  9. const hobby1 = 'sleep sleep sleep'
  10. // About user's hobby2
  11. const hobby2 = 'eat eat eat'
  12. const dosomething = (something) => 'I like' + something + '.'
  13. export default dosomething
  14. export { basicInfor, hobby1, hobby2 }
  1. // index.js
  2. // import { basicInfor, hobby1, hobby2 }, dosomething from './basic.js'; Syntax Error
  3. import dosomething, {basicInfor, hobby1, hobby2} from './basic.js'
  4. console.log(basicInfor);
  5. console.log(hobby1);
  6. console.log(hobby2);
  7. 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. NOIP2017 D2T3 题解

    题面 这种数据范围不是乱搞dfs就是乱搞状压DP 首先应该通过任一方式求出a和b的值: 任意一条抛物线只用两头猪就可以确定,所以我们N^2枚举,并把在这两头猪的抛物线上的猪都存进状态state[i][ ...

  2. 【GDOI】2018题目及题解(未写完)

    我的游记:https://www.cnblogs.com/huangzihaoal/p/11154228.html DAY1 题目 T1 农场 [题目描述] [输入] 第一行,一个整数n. 第二行,n ...

  3. 多表表与表关系 增删改查 admin

    今日内容 多表表与表关系 增删改查表数据 admin 多表操作 表与表关系 默认指向主键 可能是隐藏主键 djamgo1.1默认级联(models. SET NULL解除级联) 一对一 先建立少的一方 ...

  4. sql server isnull函数

    isnull函数 --ISNULL() 函数用于规定如何处理 NULL 值 语法:SELECT ISNULL(check_expression, replacement_value) --check_ ...

  5. nginx相关知识点

    1.nginx -V 可以查看nginx的安装目录等目录信息 2.nginx -v 查看版本 3.路径 /usr/local/etc/nginx/nginx.conf (配置文件路径) /usr/lo ...

  6. QT多线程同步之QWaitcondition

    使用到多线程,无可避免的会遇到同步问题,qt提供几种同步线程的方法,在这里讲一下QWaitcondition的简单使用. 一.QWaitcondition,是通过一个线程达到某种条件来唤起另一个线程来 ...

  7. 网络库Alamofire使用方法

    Github地址 由于Alamofire是swift网络库,所以,以下的所有介绍均基于swift项目 导入Alamofire 以下为使用cocoapods导入,其余的方式请参考官网 source 'h ...

  8. Linux版本显示和区别32位还是64位系统

    查看已经安装的Linux版本信息 1.cat /etc/issue 查看版本 [root@master master]# cat /etc/issue \S Kernel \r on an \m 2. ...

  9. Layedit 编辑页面赋值

    1.编辑页面 $("[name=Experience]").val(data.Experience);//直接赋值然后再进行build experience = layedit.b ...

  10. shell脚本中的一些特殊符号

    在shell中常用的特殊符号罗列如下:  # ;   ;; . , / \\ 'string'| !   $   ${}   $? $$   $*  \"string\"* **  ...