zoukankan      html  css  js  c++  java
  • JS Module

    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....
    小小的天有大大的梦想
  • 相关阅读:
    第九章 统一连贯的段落 《英语科技写作(文法与修辞原则)》by 方克涛
    第八章 通顺的句子 《英语科技写作(文法与修辞原则)》by 方克涛
    CMake使用操作解析参考1
    发一个SharePoint修改好式样的菜单导航
    Jquery Ajax学习实例4向WebService发出请求,返回实体对象的异步调用
    修改SharePoint网站最大上载大小时有时需要关注的事项
    轻松Ajax.net实例教程13_TextBoxWatermarkExtender(按AjaxControlToolkit字母排序)
    Jquery Ajax学习实例2向页面发出请求,返回JSon格式数据
    Jquery Ajax学习实例3向WebService发出请求,调用方法返回数据
    CSS语法学习总结
  • 原文地址:https://www.cnblogs.com/VeraYao/p/12006302.html
Copyright © 2011-2022 走看看