当前位置:网站首页>es6和commonjs对导入导出的值修改是否影响原模块
es6和commonjs对导入导出的值修改是否影响原模块
2022-07-29 04:19:00 【天落枫】
都说es6的导出是引用,commonjs导出是值拷贝,所以我想测试一下:
commonjs
commonjs对引用类型导出:对导出的引用修改,会改变模块的值,且引用了该模块的模块都可以获取最新值
// 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);
结果为:
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对基本数据类型导出:外部修改无效(会脱离指向),内部的修改,无法反映到外部!
// 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);
结果为:
a default1
b default1
a default1
b default1
a default1
b default1
a 6
b default1
es6
对于es6导出的引用类型数据,任何一个地方的更改都会影响别的地方的数据!
// 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);
结果:
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'}
换成基本类型数据,测试结果:无法在外部改变引入模块的值,因为是只读属性引入的,但是内部改变后可以被外部非默认导出获取到最新值!
// 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"; // 报错!提示常量无法赋值
y = "2"; // 报错!提示常量无法赋值
}, 2000);
结果:
a default1 default1
b default1 default1
a default1 default1
b default1 default1
报错了。。。
a default1 default1
b default1 default1
a 4 4
b default1 4
总结
- 在模块导出基本类型数据时,
require方式无法获取内部最新数据而且修改变量值会脱钩,import方式外部无法修改模块的数据(只读),但是非默认导出属性可以获取内部最新值。 - 在模块导出引用类型数据时,外部和内部的修改都可以被观测到!
边栏推荐
- Object detection: object_ Detection API +ssd target detection model
- Cad2020 introductory learning (2021.4.13)
- MPU6050
- 不会就坚持71天吧 链表排序
- It won't last for 70 days. The k-largest number in the array
- 15.federation
- Pat a1069/b1019 the black hole of numbers
- 不会就坚持70天吧 数组中第k大的数
- Won't you just stick to 62 days? Sum of words
- Interview notes of a company
猜你喜欢

15.federation

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

10. Fallback message
![[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation](/img/4b/150689d5e4809ae66a4297915ecd0c.png)
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation

不会就坚持69天吧 合并区间

No, just stick to it for 59 days

不会就坚持66天吧 权重生成随机数

Svg -- loading animation

MySQL gets the maximum value record by field grouping

不会就坚持68天吧 狒狒吃香蕉
随机推荐
The table of antd hides the pager when there is only one page
Pytoch distributed training
编译与链接
Record of problems encountered in ROS learning
异常解决:cococaption包出现找不到edu.stanford.nlp.semgraph.semgrex.SemgrexPattern错误
Don't insist on 66 days. Weight generates random numbers
What the hell is this error? It doesn't affect the execution result, but it always reports errors when executing SQL... Connecting maxcomputer uses
11. Backup switch
[kvm] create virtual machine from kickstart file
How to execute insert into select from job in SQL client
15.federation
Back propagation process of manual BP neural network
通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值
小程序:区域滚动、下拉刷新、上拉加载更多
Openfeign asynchronous call problem
Fuzzy query of SQL
信号处理中的反傅里叶变换(IFFT)原理
不会就坚持69天吧 合并区间
How to query the submission number of a version
14. Haproxy+kept load balancing and high availability