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. 什么是云数据库RDS PPAS 版

    云数据库PPAS版,是阿里云与EnterpriseDB公司合作基于PostgreSQL高度兼容Oracle语法的数据库服务,为用户提供易于操作的迁移工具,兼容范围涵盖:PL/SQL.数据类型.高级函数 ...

  2. AcWing登山

    这是2006北大举办的ACM的一道题. 题意为:给定景点海拔高度,队员们不去游览相同高度的景点,一开始往上爬,一但往下爬就不能再向上爬,求最多可以游览多少个景点.那么我们可以得到一个结论:以一个最高点 ...

  3. 使用Visual Studio 2019--调试汇编32位代码的详细步骤

    声明:本文使用32位masm,代码与16位,64位不同 ------------------------------------------------------------------------ ...

  4. thinkphp框架部署出现404解决

    1:虚拟机配置文件修改: location / { index index.php index.html; if (!-e $request_filename) { rewrite ^/index.p ...

  5. Fire Net(HDU-1045)(匈牙利最大匹配)(建图方式)

    题意 有一个 n*n 的图,. 代表空白区域,X 代表墙,现在要在空白区域放置结点,要求同一行同一列只能放一个,除非有墙阻隔,问最多能放多少个点 思路 只有在墙的阻隔情况下,才会出现一行/列出现多个点 ...

  6. 帝国 cms 修改登录次数的两种方法

    1.找到数据库表 注:我把这里的5改成50了. 2.找打e ==>> config ==>>  config.php ==>> loginnum的5修改一下即可

  7. Hadoop网页监控配置

    接之前的内容http://www.cnblogs.com/jourluohua/p/8734406.html 在之前那的内容中,仅实现了Hadoop的安装和运行,距离实际使用还有很远.现在先完成一个小 ...

  8. C语言typedef详解

    原文链接 C语言允许用户使用 typedef 关键字来定义自己习惯的数据类型名称,来替代系统默认的基本类型名称.数组类型名称.指针类型名称与用户自定义的结构型名称.共用型名称.枚举型名称等.一旦用户在 ...

  9. 在java中使用solr7.2.0 新旧版本创建SolrClient对比

    在Java中使用solr 版本7.2.0 solrj已经更新到了7.2.0,新版本solr获取SolrClient的方式也和之前旧版本有所不同 solr6.5开始不推荐直接使用HttpSolrClie ...

  10. 多线程与UI操作(二)

    为了让程序尽快响应用户操作,在开发Windows应用程序时经常会使用到线程.对于耗时的操作如果不使用线程将会是UI界面长时间处于停滞状态,这种情况是用户非常不愿意看到的,在这种情况下我们希望使用线程来 ...