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 ...
随机推荐
- hdfs基本文件操作
编程实现下列要求: 1.创建一个自己姓名首字母的文件夹 2.在文件夹下创建一个hdfstext1.txt文件,项文件内输入“班级学号姓名HDFS课堂测试”的文字内容: 3.在文件夹下在创建一个好的fs ...
- Jconsole与Jmx 分析JVM状况(下) 转
出处: Jconsole与Jmx 分析JVM状况(下) 线程(ThreadMXBean ) 从 Jconsole 画面取得线程画面如下: 左下角列出了所以正在运行的线程.通过点击某个线程,右下脚可以看 ...
- Codeforces 1196C. Robot Breakout
传送门 维护合法区域的四个边 $xa,ya,xb,yb$ 表示在以 $(xa,ya)$ 为左下角,以 $(xb,yb)$ 为右上角的矩形内的点都是合法答案 对于一个起点 $(x,y)$,如果没法往左, ...
- Codeforces 1221E. Game With String
传送门 首先每一段连续的 $...$ 都是互不影响的,所以可以一段段考虑 考虑最简单的情况,此时每一段都大于等于 $a$ 并且小于 $2b$ ,那么每一段都只能放一次,胜负直接根据段数即可得到答案 考 ...
- Collection接口的子接口——Set接口
https://docs.oracle.com/javase/8/docs/api/java/util/Set.html public interface Set<E> extends ...
- js文件的框架
Ext.define("BeidaSoft.SFJCGL.rcjwgl.bdgl.BdglGrid", { extend : "BeidaSoft.XTGL.base.Q ...
- 今天给大家分享一下js中常用的基础算法
今天给大家分享一下js中常用的基础算法,废话不多说,直接上代码: 1.两个数字调换顺序 ,b= function fun(a,b){ b = b - a ;// a = 2 ; b = 2 a = a ...
- Nginx安装目录详解
Nginx安装目录详解 1. 查看有关nginx的所有目录列表,输入命令 rpm -ql nginx 可以查看有关nginx目录信息,但是注意 这种命令只能是在基于yum安装的方式才可以. 2. 下 ...
- Oracle DBA 学习总结
对于学习Oracle 数据库,应该先要了解Oracle 的框架.它有物理结构(由控制文件.数据文件.重做日志文件.参数文件.归档文件.密码文件组成) ,逻辑结构(表空间.段.区.块),内存分配( SG ...
- mysql 命令行登录详解
mysql -?或者是mysql --help或者是mysql -I显示mysql命令的选项. #最全的mysql登录方式: mysql -hIP -uUSENME -pPWD -PPORT DBNA ...