当前位置:网站首页>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方式外部无法修改模块的数据(只读),但是非默认导出属性可以获取内部最新值。 - 在模块导出引用类型数据时,外部和内部的修改都可以被观测到!
边栏推荐
- Database SQL statement realizes function query of data decomposition
- Jenkins 参数化构建中 各参数介绍与示例
- “蔚来杯“2022牛客暑期多校训练营2 H
- rman不标记过期备份
- [k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
- Machine vision series 3:vs2019 opencv environment configuration
- Don't stick to it for 68 days. Baboons eat bananas
- Pytoch distributed training
- 不会就坚持58天吧 实现前缀树
- How to execute insert into select from job in SQL client
猜你喜欢

AssertionError(“Torch not compiled with CUDA enabled“)

Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)
![[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

Not for 60 days, magical dictionary

15.federation

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

Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment

Fu Yingna: Yuan universe is the new generation of Internet!

14. Haproxy+kept load balancing and high availability

不会就坚持59天吧 替换单词
随机推荐
[Openstack] keystone,nova
Down sampling and up sampling
It won't last for 70 days. The k-largest number in the array
Won't you just stick to 62 days? Sum of words
The pit I walked through: the first ad Sketchpad
How to set the SQL execution timeout for flick SQL
Applet: Area scrolling, pull-down refresh, pull-up load more
How to query the submission number of a version
6.pytest生成allure报告
Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment
(.*?) regular expression
C语言:枚举知识点总结
全屋WiFi方案:Mesh路由器组网和AC+AP
Pytorch GPU and CPU models load each other
Why do I delete the original record (OP d) and then add a new one in Kafka when I update MySQL data
不会就坚持63天吧 最大的异或
AssertionError(“Torch not compiled with CUDA enabled“)
Interview notes of a company
Codeforces Round #810 (Div. 2) D. Rain (线段树差分)
openFeign异步调用问题