当前位置:网站首页>New modularity in ES6
New modularity in ES6
2022-07-28 05:16:00 【M78_ Domestic 007】
Before introducing modularity , Introduce the outside js File by <script src=" File path "></script>
Imported through this method js file ,js Global variables declared in the file Will pollute the present html Global of files .
stay ES6 when , Modularity is introduced to solve this defect , Each module has its own context , The variables declared in each module are local variables , Does not contaminate the global scope .
Modular features :
1、 Avoid global pollution of variables ;
2、ES6 The module automatically turns on strict mode , Whether or not you add 'use strict';
3、 Various types of variables can be imported and exported in the module , Such as function , object , character string , Numbers , Boolean value , Class etc. ;
4、 Each module is loaded only once ( Is a singleton ), If you load the same file in the same directory , Read directly from memory .
Modularity is divided into import and export modules
export (export)
There are two kinds of export Separate export and default Export ( And default A combination of )
Those exported separately must be imported on demand with the exported identifier , There can be multiple separate exports ;
export default The default is derived Only one or... Can be exported 0 individual .
<script>
// There can be many separate exports , You can export all kinds of data
export var a=10
export var fn=function() {}
export var arr=[1,2,3]
export var obj={name:"xl",age:11}
// wait
// The default export can only have one
export default {a,fn,arr,obj}
// What is exported here is an object , It contains every previous item of data , This is written in es6 New object literal , Variables can be written directly into objects {a} Is equal to {a:10}
</script>When a module is exported , You need to use the data of this module , Import the exported module in the file .
Import (import)
import And from Associated use , here script Labeled type Must be set to module ,
Such as <script type="module"></script>
Use format :
import Received identifier form " Path to export file "
explain : If it is exported separately , Here, the identifier of the receiving setting must be the same as the exported identifier and written in {} Inside , The exported by default is not required .
Code instructions
file 1.js:
export var a=10
file 2:
<script type="module">
import {a} from "./ file 1.js" // Suppose you are in the same folder
// You can use a 了
console.log(a) //10
</script>When there are many data exported separately , We don't have to write much import, Just according to our needs , Write the exported identifier in { } Separated by commas, import on demand , Code instructions :
file 3.js:
var obj={name:"karen"}
export var num=100
export function tool () {return obj}
export var arr=[1,2,3]
export var str = "hello world"
file 4:
<script type="module">
// Import according to the data we need , Note that the identifier here should also be the same
import {num,tool,str} from "./ file 3.js" // Suppose you are in the same folder
console.log(num) //100
console.log(tool()) //{name1:"karen"}
console.log(str) //hello world
</script>When we export an object data by default , I can use A custom identifier receives , When you need the data inside , Directly use grammar sugar point grammar to get .
for example :
file 5.js:
var obj={name:"karen"}
var num=100
function tool () {return obj}
var arr=[1,2,3]
var str = "hello world"
export default {num,tool,arr,str}
file 6:
<script type="module">
import con from "./ file 5.js" // Suppose you are in the same folder use con The recipient
// Use num data
console.log(con.num) //100
// Use str data
console.log(con.str) //hello world
</script>边栏推荐
- MySQL date and time function, varchar and date are mutually converted
- Professor dongjunyu made a report on the academic activities of "Tongxin sticks to the study of war and epidemic"
- What is the core value of testing?
- FreeRTOS personal notes - task notification
- Win10 machine learning environment construction pycharm, anaconda, pytorch
- 【ARIXV2204】Neighborhood attention transformer
- Dell remote control card uses ipmitools to set IPMI
- How to successfully test php7.1 connecting to sqlserver2008r2
- Activation functions sigmoid, tanh, relu in convolutional neural networks
- After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?
猜你喜欢

Simulink automatically generates STM32 code details

微服务故障模式与构建弹性系统

7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格

Microservice failure mode and building elastic system

驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam

How to quickly locate bugs? How to write test cases?

Win10 machine learning environment construction pycharm, anaconda, pytorch

flink思维导图

Summary and review of puppeter

【ARXIV2204】Vision Transformers for Single Image Dehazing
随机推荐
HDU 3585 maximum shortest distance
The go zero singleton service uses generics to simplify the registration of handler routes
数据库日期类型全部为0
Clickhouse pit filling note 2: the join condition does not support non equal judgments such as greater than and less than
I've been in an outsourcing company for two years, and I feel like I'm going to die
The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies
Reading sdwebimage source code Notes
[learning record] data enhancement 1
How to send and receive reports through outlook in FastReport VCL?
What should testers know about login security?
MySQL(5)
Testcafe provides automatic waiting mechanism and live operation mode
数据安全逐步落地,必须紧盯泄露源头
FreeRTOS learning (I)
【ARXIV2204】Simple Baselines for Image Restoration
【SLAM】LVI-SAM解析——综述
【ARXIV2203】SepViT: Separable Vision Transformer
Read the paper -- a CNN RNN framework for clip yield prediction
【ARXIV2203】CMX: Cross-Modal Fusion for RGB-X Semantic Segmentation with Transformers
这种动态规划你见过吗——状态机动态规划之股票问题(中)