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 ...
随机推荐
- Ubuntu - Ubuntu应用记录(1)
1.发生dpkg status database is locked by another process 原因是包管理器没有正确关闭.需要重启计算机或者重新打开终端 输入: sudo rm /var ...
- Struts 2访问Servlet API
在servlet中可以通过servlet API来获取Session,在Struts中如何获取Session呢? 解析:将用户名放入session 两种方案 1. 与Servlet API解耦的访问方 ...
- P1828香甜的黄油
这是一道关于最短路的绿题. 题目给出一些农场,每个农场有奶牛,农场与农场之间存在边,要使所有奶牛到达其中一个农场的总距离最短,输出他们到达这个农场的距离.首先我想到了最小生成树,但我发现其实并不是,因 ...
- innodb中一颗B+树能存储多少条数据
如图,为B+树组织数据的方式: 实际存储时当然不会每个节点只存3条数据. 以InnoDB引擎为例,简单计算一下一颗B+树可以存放多少行数据. B+树特点:只有叶子节点存储数据,而非叶子节点存放的是用来 ...
- (转)Cvte提前批
1. 加密解密了解么?几种算法,讲一下你了解的(链接) 算法选择:对称加密AES,非对称加密: ECC,消息摘要: MD5,数字签名:DSA 常见加密算法 1.DES(Data Encryption ...
- [转]Opcode是啥以及如何使用好Opcache
转载链接:Opcode是啥以及如何使用好Opcache 啥是Opcode? 我们在日常的PHP开发过程中,应该经常会听见Opcache这个词,那么啥是Opcode呢? Opcache 的前生是 Opt ...
- 利用yaml文件管理资源
利用yaml配置文件管理资源 [root@master ~]# cat nginx-deployment.yaml apiVersion: apps/v1beta2 kind: Deployment ...
- kinit: Bad encryption type while getting initial credentials
描述:RHEL 6.x主机执行kinit -kt命令报如下错误 [heboan@localhost~]$ kinit -kt heboan.keytab heboan kinit: Bad encry ...
- css简单动画(transition属性)
一.对transition属性的认识 1.transition 属性是一个简写属性,可用于设置四个过渡属性:transition-property 过渡效果的 CSS 属性的名称(height ...
- Linux和Windows双系统下Windows系统插入耳机没有声音
我的笔记本装了Windows7和Debian双系统后,在Windows7下,插入耳机竟然没有声音. 按常规思路分析:首先考虑是耳机问题还是笔记本电脑问题.确定耳机没问题后问题就在笔记本身上了.而问题在 ...