当前位置:网站首页>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
方式外部无法修改模块的数据(只读),但是非默认导出属性可以获取内部最新值。 - 在模块导出引用类型数据时,外部和内部的修改都可以被观测到!
边栏推荐
- Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)
- C language: talking about various complex statements
- 6.pytest生成allure报告
- 优炫数据库有办法查到主集群每天传给备集群的日志量吗?
- 编译与链接
- 12.优先级队列和惰性队列
- 通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值
- 淘宝商品详情接口(商品详情页面数据接口)
- Openfeign asynchronous call problem
- Can you really write restful API?
猜你喜欢
Not for 61 days. The shortest word code
6.pytest生成allure报告
Deep learning training strategy -- warming up the learning rate
伏英娜:元宇宙就是新一代互联网!
从淘宝,天猫,1688,微店,京东,苏宁,淘特等其他平台一键复制商品到拼多多平台(批量上传宝贝详情接口教程)
Can you really write restful API?
Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)
How to solve the problem of store ranking?
Code or script to speed up the video playback of video websites
rman不标记过期备份
随机推荐
Applet: Area scrolling, pull-down refresh, pull-up load more
[kvm] install KVM
Not for 61 days. The shortest word code
6.pytest生成allure报告
The structure pointer must be initialized, and the pointer must also be initialized
Fuzzy query of SQL
Sequence list and linked list
[k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
Cad2020 introductory learning (2021.4.13)
Svg -- loading animation
Shielding ODBC load balancing mode in gbase 8A special scenarios?
C language force buckle question 61 of the rotating list. Double ended queue and construction of circular linked list
Compilation and linking
C语言:结构体简单语法总结
AssertionError(“Torch not compiled with CUDA enabled“)
Use of torch.optim optimizer in pytorch
Down sampling and up sampling
Semantic segmentation correlation
12. Priority queue and inert queue
不会就坚持66天吧 权重生成随机数