当前位置:网站首页>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 !
边栏推荐
- C语言:typedef知识点总结
- es6和commonjs对导入导出的值修改是否影响原模块
- C语言:联合体知识点总结
- Back propagation process of manual BP neural network
- Won't you just stick to 69 days? Merge range
- Pytorch fixed random seed & recurrence model
- 不会就坚持64天吧 查找插入位置
- C language force buckle question 61 of the rotating list. Double ended queue and construction of circular linked list
- 不会就坚持67天吧 平方根
- Openfeign asynchronous call problem
猜你喜欢

9. Delay queue

Back propagation process of manual BP neural network

No, just stick to it for 59 days

MySQL - deep parsing of MySQL index data structure

Machine vision series 3:vs2019 opencv environment configuration

12. Priority queue and inert queue

visio画网格

SVG--loading动画

Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)

Introduction and examples of parameters in Jenkins parametric construction
随机推荐
从淘宝,天猫,1688,微店,京东,苏宁,淘特等其他平台一键复制商品到拼多多平台(批量上传宝贝详情接口教程)
C language: summary of consortium knowledge points
mpc5744p简介与OpenSDA固件更新
Code or script to speed up the video playback of video websites
论pyscript使用感想(实现office预览)
Kotlin's list, map, set and other collection classes do not specify types
RMAN do not mark expired backups
Why is it necessary to scale the attention before softmax (why divide by the square root of d_k)
openFeign异步调用问题
Niuke IOI weekly 27 popularity group
[k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
Interview notes of a company
MySQL gets the maximum value record by field grouping
Don't stick to it for 68 days. Baboons eat bananas
TypeError: Cannot read properties of undefined (reading ‘then‘)
不会就坚持67天吧 平方根
9.延迟队列
顺序表和链表
Pytorch GPU and CPU models load each other
Wechat applet parameter transfer