当前位置:网站首页>ES6--模块化
ES6--模块化
2022-07-13 17:35:00 【爱喝珍珠奶茶】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
1.模块化分类
ES6 引入了模块化,其设计思想是在编译时就能确定模块的依赖关系,以及输入和输出的变量。
ES6 的模块化分为导出(export) 与导入(import)两个模块。
2.模块化特点
(1)ES6 的模块自动开启严格模式,不管你有没有在模块头部加上 use strict;。
(2)模块中可以导入和导出各种类型的变量,如函数,对象,字符串,数字,布尔值,类等。
(3)每个模块都有自己的上下文,每一个模块内声明的变量都是局部变量,不会污染全局作用域。
(4)每一个模块只加载一次(是单例的), 若再去加载同目录下同文件,直接从内存中读取。
3.export导出
与default关联使用,并且一个js模块中只能有一个export default语句
var obj={name:"karen"}
export var num=100
export function tool () {return obj}
// 单独导出的 必须用导出的标识符来按需导入,可以有多个单独导出
// 默认导出 在这个文件中只能有一个或者0个
var a=20
var arr=[100,203,4]
var fm=()=>{}
export default {a,arr,fm,num,tool}
4.import导入
与from关联使用,此时script标签的type必须设置为module
单例模式:多次重复执行同一句 import 语句,那么只会执行一次,而不会执行多次。import 同一模块,声明不同接口引用,会声明对应变量,但只执行一次 import
举例:
<script type="module">
import People from './js/myModule.js';
let p = new People();
p.say();
</script>5. 把js脚本写在页面中3种方式
1.行内式:js引擎要去执行的标签的某些(事件)属性中
2.嵌入式
3.导入 src的地址是一个js的路径 会加载js编码(jsonp)
6.按需引入、按需导入
import x,{num,tool} from "./src/model1.js"
console.log(num)
console.log(tool)
console.log(obj)
var re=tool()
console.log(re)
console.log(x.arr)
import {tool} from "./src/model1.js"
import x from "./src/model1.js"
x.tool
import Hq from './src/model1.js'
console.log(Hq.Photo)
import {Photo as a} from './src/model1.js'
let a=Photo
console.log(Photo.name)
边栏推荐
- Mysql 实战45讲
- 数据类型与约束
- mysql中all用法和any的用法和内连接和外连接,全外连接,联合查询,自连接
- fenxi
- 解决IDEA2020连接MySQL8出现unable to load authentication plugin ‘caching_sha2_password‘异常
- 万建峰老师 干货分享2022年7月13日
- ES6新增的模块化
- [tensorflow2] implementation of gradient inversion layer (GRL) and domain antagonism training neural network (Dann)
- 蓝桥杯单片机第13届省赛题
- Implementation principle and application practice of Flink CDC mongodb connector
猜你喜欢

Design and implementation of an eight bit compensator based on logisim

快手实时数仓保障体系研发实践

Changement de style TW du script singe huileux
![[untitled]](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[untitled]

Implementation principle and application practice of Flink CDC mongodb connector

解决IDEA2020连接MySQL8出现unable to load authentication plugin ‘caching_sha2_password‘异常

Dynamic programming leetcode509 Fibonacci number
c语言中的输入输出函数之printf函数
图片清晰度问题

Based on the use of PageHelper paging plug-in in SSM project (easy to use)
随机推荐
Register the implementation.
Mysql基本操作整理
New progress in the construction of meituan's Flink based real-time warehouse platform
基于logisim的八位求补器的设计与实现
Which company is the safest to open a futures account?
2022暑期实践(第一周)
The 13th provincial competition of Bluebridge cup single chip microcomputer
At 19:00 p.m. on Thursday, the third live broadcast of knowledge empowerment - control panel function realization of openharmony smart home project
sql_server2014下载与安装
P1664 clock in every day and feel good [getting started]
[interview: concurrent Article 12: multithreading: thread eight locks]
美团基于 Flink 的实时数仓平台建设新进展
Résoudre le problème de l'échec de l'écho en temps opportun après le téléchargement de l'image du projet SSM
"Cheating" big factories: seven product development strategies that can be referred to
Implementation principle and application practice of Flink CDC mongodb connector
ES6--箭头函数
Lu te software Qi Jun: cloud native helps SaaS business tenants to isolate efficiently
系统总出故障怎么办,或许你该学学稳定性建设
P1664 每日打卡心情好【入门】
Basic part of C language: minesweeping


