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 ...
随机推荐
- MyISAM与InnoDB的索引差异
数据库的索引分为主键索引(Primary Index)与普通索引(Secondary Index).InnoDB和MyISAM是怎么利用B+树来实现这两类索引的,又有什么差异呢?一.MyISAM的索引 ...
- TCP三次握手与四次挥手详解(最全面)
目录 TCP的三次握手与四次挥手 TCP报文段的首部格式 TCP的工作原理 TCP 的流量控制 TCP的拥塞控制 拥塞控制与流量控制的关系 拥塞控制所起的作用 慢开始和拥塞避免 慢开始算法的原理 三次 ...
- 福建工程学院第十四届ACM校赛B题题解
第二集,未来的我发量这么捉急的吗 题意: 有n个数,请问有多少对数字(i,j)(1<=i<j<=n),满足(a[i]^a[j])+((a[i]&a[j])<<1) ...
- Abp添加新的接口(扩展底层接口)
在https://aspnetboilerplate.com/Templates 创建项目之后,下载用Vs2019打开(vs2017不支持netcore3.0)结构如下: 一. 2. 在xx.core ...
- HDFS NFS Gateway
NFS网关支持NFSv3,并允许将HDFS作为客户端本地文件系统进行挂载.目前,NFS Gateway支持并启用以下使用模式: 用户可以通过NFSv3客户端兼容操作系统上的本地文件系统浏览HDFS文件 ...
- python 模块使用
模块使用 定义:模块就像一个工具包一样,里面有很多工具(函数.类),使用时需要通过import导入. 分类: 标准库:random.sys.os.time 第三方:就是好人已经写好的特定功能的模块,你 ...
- 1、linux基础优化
1.添加一个用户 [root@oldboy ~]# useradd oldboy [root@oldboy ~]# id oldboy uid=500(oldboy) gid=500 (oldboy) ...
- lxml:底层C语言实现、高效地处理html
介绍 lxml也是一个用于筛选指定html内容的模块,pyquery就是基于lxml. 使用lxml主要需要了解xpath xpath语法 /:在子节点里面找 //:在子子孙孙节点里面找 //div: ...
- linux centos 7安装 apache php 及mariadb
1安装Apache, PHP, MySQL以及php库组件. yum -y install httpd php mysql php-mysql 2 安装apache扩展 yum -y install ...
- 模拟赛小结:2017 China Collegiate Programming Contest Final (CCPC-Final 2017)
比赛链接:传送门 前期大顺风,2:30金区中游.后期开题乏力,掉到银尾.4:59绝杀I,但罚时太高卡在银首. Problem A - Dogs and Cages 00:09:45 (+) Solve ...