当前位置:网站首页>Whether the modification of import and export values by ES6 and commonjs affects the original module
Whether the modification of import and export values by ES6 and commonjs affects the original module
2022-07-29 04:23:00 【Tianluofeng】
All say es6 The export of is a reference ,commonjs Export is a copy of value , So I want to test :
commonjs
commonjs Yes Reference type export : Modify the exported reference , Will change the value of the module , And modules that reference this module can get the latest value
// a.js
module.exports.x = "default1";
setTimeout(() => {
module.exports.x = 6;
}, 4000);
setInterval(() => {
console.log('a',module.exports);
}, 1000);
// b.js
let a = require("./a.js");
setInterval(() => {
console.log('b',a);
}, 1000);
setTimeout(() => {
a.x = "2";
}, 2000);
The result is :
a { x: 'default1' }
b { x: 'default1' }
a { x: '2' }
b { x: '2' }
a { x: '2' }
b { x: '2' }
a { x: 6 }
b { x: 6 }
commonjs Export to basic data type : Invalid external modification ( Will be out of direction ), Internal modification , Cannot be reflected externally !
// a.js
module.exports = "default1";
setTimeout(() => {
module.exports = 6;
}, 4000);
setInterval(() => {
console.log('a',module.exports);
}, 1000);
// b.js
let a = require("./a.js");
setInterval(() => {
console.log('b',a);
}, 1000);
The result is :
a default1
b default1
a default1
b default1
a default1
b default1
a 6
b default1
es6
about es6 Derived Reference type data , Changes anywhere will affect data elsewhere !
// a.js
let x = {
name:"default1"};
export let y = {
name:"default2"};
export default x;
setTimeout(() => {
x.name = "4";
y.name = "4";
}, 4000);
setInterval(() => console.log("a", x, y), 1000);
// b.js
import x from "./a.js";
import {
y } from "./a.js";
setInterval(() => console.log("b", x, y), 1000);
setTimeout(() => {
x.name = "2";
y.name = "2";
}, 2000);
result :
a {name: 'default1'} {name: 'default2'}
b {name: 'default1'} {name: 'default2'}
a {name: 'default1'} {name: 'default2'}
b {name: 'default1'} {name: 'default2'}
a {name: '2'} {name: '2'}
b {name: '2'} {name: '2'}
a {name: '4'} {name: '4'}
b {name: '4'} {name: '4'}
Replace with basic type data , test result : The value of the incoming module cannot be changed externally , Because it is introduced by the read-only attribute , However, after internal changes, the latest value can be obtained by external non default Export !
// a.js
let x = 'default1';
export let y = 'default1';
export default x;
setTimeout(() => {
x = "4";
y = "4";
}, 4000);
setInterval(() => console.log("a", x, y), 1000);
// b.js
import x from "./a.js";
import {
y } from "./a.js";
setInterval(() => console.log("b", x, y), 1000);
setTimeout(() => {
x = "2"; // Report errors ! Prompt constant cannot be assigned
y = "2"; // Report errors ! Prompt constant cannot be assigned
}, 2000);
result :
a default1 default1
b default1 default1
a default1 default1
b default1 default1
Wrong report ...
a default1 default1
b default1 default1
a 4 4
b default1 4
summary
- Export in module Basic types of data when ,
requireThe method cannot obtain the latest internal data, and modifying the variable value will decouple ,importThe data of the module cannot be modified outside the mode ( read-only ), But the non default export attribute can get the latest internal value . - Export in module Reference type data when , Both external and internal modifications can be observed !
边栏推荐
- [hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
- 10.回退消息
- i++与++i详解
- 小程序:区域滚动、下拉刷新、上拉加载更多
- Realize the effect of univariate quadratic equation through JS. Enter the coefficients of a, B and C to calculate the values of X1 and x2
- Opengauss pre check installation
- Database SQL statement realizes function query of data decomposition
- BIO、NIO、AIO的区别和原理
- Flutter实战-请求封装(二)之dio
- Semantic segmentation correlation
猜你喜欢

It won't last for 65 days. It only appears once

Not for 60 days, magical dictionary

Won't you just stick to 62 days? Sum of words

6. Pytest generates an allure Report

Won't you just stick to 69 days? Merge range

Installation and use of stm32cubemx (5.3.0)

RMAN do not mark expired backups

9. Delay queue

No, just stick to it for 59 days

10. Fallback message
随机推荐
11. Backup switch
Introduction and examples of parameters in Jenkins parametric construction
Methods of using multiple deformations on an element
Unity基础(3)—— unity中的各种坐标系
不会就坚持59天吧 替换单词
Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
11.备份交换机
Niuke IOI weekly 27 popularity group
Common components of solder pad (2021.4.6)
String, array, generalized table (detailed)
Dabao and Erbao
Update learning materials daily
Wechat applet parameter transfer
MySQL - clustered index and secondary index
不会就坚持66天吧 权重生成随机数
Shielding ODBC load balancing mode in gbase 8A special scenarios?
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
C language: structure simple syntax summary
C语言力扣第61题之旋转链表。双端队列与构造循环链表