当前位置:网站首页>【实践出真理】import和require的引入方式真的和网上说的一样吗
【实践出真理】import和require的引入方式真的和网上说的一样吗
2022-07-06 21:51:00 【Concise200】
import和require
今天做笔试题的时候做到了一道题,如图所示
正确答案如下:
按网上的说法是正确的,因为require导入的就是类似于浅拷贝的操作(如果有北鼻不了解浅拷贝,可以查看一文教你区分赋值、浅拷贝和深拷贝)
但是网上也说
require通过module.exports导出的值就不能再变化,import通过export导出的值可以改变;
真的是这样的吗???
浅浅实践一波
// a.js
let a = 1
let b = {
}
setTimeout(() => {
a = 2
b.b = 2
}, 100)
export default{
a,
b
}
import a from './a.js'
console.log(a.a)
console.log(a.b)
setTimeout(() => {
console.log(a.a)
console.log(a.b)
}, 500)
按网上的结果,得到的答案应该是
1
{}
2
{ b: 2 }
可是实际上却是
在上面的表现中,二者都类似 浅拷贝 的操作,并不和网上说的一样a会变成2。
可是如果采用按需导入,那基本数据类型和引用数据类型都是一起变化的
let a = 1
let b = {
}
setTimeout(() => {
a = 2
b.b = 2
}, 100)
export{
a,
b
}
// 或者这么写
/*export let a = 1 export let b = {} setTimeout(() => { a = 2 b.b = 2 }, 100)*/
import {
a,b} from './a.js'
console.log(a)
console.log(b)
setTimeout(() => {
console.log(a)
console.log(b)
}, 500)
结果如图所示
总结:
- 默认导出 和 require都是浅拷贝
- 按需导出才是引用,不管是基本数据类型还是引用类型,只要
导出方
发生改变,导入方
就会改变。
边栏推荐
- 数学分析_笔记_第10章:含参变量积分
- 【写给初发论文的人】撰写综述性科技论文常见问题
- Hangzhou Electric 3711 binary number
- Use facet to record operation log
- 2022中青杯C题城市交通思路分析
- [untitled]
- Optimization of channel status offline of other server devices caused by easycvr cluster restart
- B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
- JetBrain Pycharm的一系列快捷键
- 过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
猜你喜欢
buildroot的根文件系统提示“depmod:applt not found”
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
[team learning] [34 sessions] Alibaba cloud Tianchi online programming training camp
Network Security Learning - Information Collection
[team learning] [34 issues] scratch (Level 2)
SSM+jsp实现仓库管理系统,界面那叫一个优雅
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
The most complete deployment of mongodb in history
Easycvr cannot be played using webrtc. How to solve it?
案例大赏:英特尔携众多合作伙伴推动多领域AI产业创新发展
随机推荐
kivy教程之设置窗体大小和背景(教程含源码)
Win11玩绝地求生(PUBG)崩溃怎么办?Win11玩绝地求生崩溃解决方法
Easycvr cannot be played using webrtc. How to solve it?
【自动化经验谈】自动化测试成长之路
Nanopineo use development process record
2022中青杯C题城市交通思路分析
Different meat customers joined hands with Dexter to launch different hamburgers in some stores across the country
Redis source code learning (31), dictionary learning, dict.c (1)
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
视频融合云平台EasyCVR视频广场左侧栏列表样式优化
AI 落地新题型 RPA + AI =?
论文上岸攻略 | 如何快速入门学术论文写作
Win11远程桌面连接怎么打开?Win11远程桌面连接的五种方法
軟件測試之網站測試如何進行?測試小攻略走起!
Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
How to write a resume that shines in front of another interviewer [easy to understand]
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
True global ventures' newly established $146million follow-up fund was closed, of which the general partner subscribed $62million to invest in Web3 winners in the later stage
Both primary and secondary equipment numbers are 0
機器人(自動化)課程的持續學習-2022-