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
- js
// 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:
- 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.- 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
- js
// 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:
- In a module file,
export defaultonly appear once, butexportcan appear many times.- When we use
export default, we don't need braces. On the contrary,exportmust be bracketed. Similarly, the usage of import is same to export, but there is one thing to note that isimport {default as name} from './basic.jscan be abbreviated toimport name from './basic.js.export const name = ...is corret. Instead,export default const name = ...is wrong.- Last but not least, in a module file, the value export from
export namecan be changed, but the other value export fromexport default namecannot be changed.
II.III Rename Export and Import(How to avoid naming conflicts?)
Please wait patiently....
JS Module的更多相关文章
- node.js module初步理解
在开发一个复杂的应用程序的时候,我们需要把各个功能拆分.封装到不同的文件,在需要的时候引用该文件.没人会写一个几万行代码的文件,这样在可读性.复用性和维护性上都很差,几乎所有的编程语言都有自己的模块组 ...
- 如何發佈一個完整Node.js Module
本文會透過以下幾個段落,讓各位一步一步學習如何寫一個自已的Node.js Module並且發佈到npm package上 Node.js Module 結構 我們先建立一個 NodeModuleDem ...
- (转)Node.js module.exports与exports
本文转自Node.js module.exports与exports 作者: chemdemo 折腾Node.js有些日子了,下面将陆陆续续记录下使用Node.js的一些细节. 熟悉Node.js的童 ...
- 创建并发布node.js module
创建node.js module. 创建一个文件夹,用来存放module. Cd到新创建的文件夹,运行npm init,会提示输入package的信息. 可以按照这个视频的来输入.Test com ...
- Node.js & module system
Node.js & module system Node.js v10.9.0 Documentation https://nodejs.org/api/modules.html#module ...
- 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 ...
- Node.js & module.exports & exports
Node.js & module.exports & exports https://www.cnblogs.com/xgqfrms/p/9493550.html exports &a ...
- node.js module初步理解-(转载)
在开发一个复杂的应用程序的时候,我们需要把各个功能拆分.封装到不同的文件,在需要的时候引用该文件.没人会写一个几万行代码的文件,这样在可读性.复用性和维护性上都很差,几乎所有的编程语言都有自己的模块组 ...
- Node.js module.exports和exports的区别
require 用来加载代码,而 exports 和 module.exports 则用来导出代码,从接触node.js就不会它们两陌生,上代码: foo.js exports.a = functio ...
随机推荐
- springBoot中tomcat默认端口修改
springboot在启动tomcat的默认端口是8080,在实际开发中,应客户要求必须使用80端口. 研究springboot后发现有两种方式可以实现修改tomcat的端口 第一.直接修改appli ...
- 基于rabbitmq的Spring-amqp基本使用
目录 1. 依赖和配置 添加AMQP的启动器: 在application.yml中添加RabbitMQ地址: 2. 监听者 3. AmqpTemplate 4. 测试代码 Spring-amqp是对A ...
- Git上传相关资料
############ssh key及 配置信息############# 设置Git的user name和email: $ git config --global user.name " ...
- Java 条件语句 if else
一个 if 语句包含一个布尔表达式和一条或多条语句. 语法 if 语句的语法如下: if(布尔表达式) { //如果布尔表达式为true将执行的语句 } 如果布尔表达式的值为 true,则执行 if ...
- redis 学习(6)-- 集合类型
redis 学习(6)-- 集合类型 set 结构 无序 无重复 集合间操作 set 集合内操作 命令 含义 sadd key memebr1 [member2...] 向集合中添加一个或多个成员 s ...
- 树莓派USB存储设备自动挂载并通过脚本实现自动拷贝,自动播放视频,脚本自动升级等功能
需求:首先需要树莓派自动挂载USB设备,然后扫描USB指定目录下文件,将相关文件拷贝至树莓派指定目录,然后通过omxplayer循环播放新拷贝文件视频 1. 树莓派实现USB存储设备自动挂载 树莓派U ...
- try catch和finally
在C#中这三个关键字用于处理异常. 这三个关键字try是必定要用的,要不然就失去了意义.然后catch和finally可以不用但是要注意遵循原则. 存在一个或多个catch的时可以不用finally, ...
- 第三章 联接查询 T-SQL语言基础
联接查询 sql server 2008支持四种表运算符----JOIN,APPLY,PIVOT,UNPIVOT. JOIN表运算符是ANSI标准,而APPLY,PIVOT,UNPIVOT是T-SQL ...
- git简易操作手册
从远程仓库新建 $ git clone https://github.com/xxxr/Repository.git $ cd Repository/ $ git config --global us ...
- js 数据类型的判断
1. typeof 运算符 typeof 可以判断基本数据类型: typeof 123; // "number" typeof 'abc'; // "string&quo ...