当前位置:网站首页>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 ,
require
The method cannot obtain the latest internal data, and modifying the variable value will decouple ,import
The 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 !
边栏推荐
- 淘宝商品详情接口(商品详情页面数据接口)
- Database SQL statement realizes function query of data decomposition
- Incubator course design (April 12, 2021)
- Pytoch distributed training
- 不会就坚持63天吧 最大的异或
- settings.xml
- C language force buckle question 61 of the rotating list. Double ended queue and construction of circular linked list
- oracle 更新和删除数据
- 不会就坚持70天吧 数组中第k大的数
- Model tuning, training model trick
猜你喜欢
Machine vision Series 2: vs DLL debugging
MySQL - deep parsing of MySQL index data structure
开课!看smardaten如何分解复杂业务场景
WebRTC实现简单音视频通话功能
Introduction and examples of parameters in Jenkins parametric construction
Won't you just stick to 69 days? Merge range
Beginner: array & String
No, just stick to it for 59 days
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
HC06 HC05 BT
随机推荐
The third ACM program design competition of Wuhan University of Engineering
Fuzzy query of SQL
LeetCode_ Stack topics
全屋WiFi方案:Mesh路由器组网和AC+AP
Taobao product details interface (product details page data interface)
读懂 互联网巨头 【中台之战】 以及 中台 发展思维
The principle of inverse Fourier transform (IFFT) in signal processing
Exception resolution: error of not finding edu.stanford.nlp.semgraph.semgrex.semgrexpattern in cococaption package
不会就坚持63天吧 最大的异或
Common components of solder pad (2021.4.6)
Machine vision series 3:vs2019 opencv environment configuration
It won't last for 65 days. It only appears once
Sequence list and linked list
不会就坚持64天吧 查找插入位置
Whole house WiFi solution: mesh router networking and ac+ap
openFeign异步调用问题
12.优先级队列和惰性队列
Visio draw grid
使用容器部署Jenkins
淘宝商品详情接口(商品详情页面数据接口)