gpt4 book ai didi

javascript - node js 中的 module.exports 是否创建导出对象或函数的浅拷贝或深拷贝?

转载 作者:搜寻专家 更新时间:2023-11-01 00:12:26 24 4
gpt4 key购买 nike

例如,

如果我有 2 个模块 try1.js 和 try2.js

try1.js
module.exports.msg = "hello world";

try2.js
try1 = require('./try1');
try1.msg = "changed message";

try2.js中msg内容的变化会影响try1.js中msg的try值吗?

最佳答案

根本没有复制。 module.exports 是一个对象,该对象直接共享。如果您修改该对象的属性,那么所有加载该模块的人都会看到这些更改。

Does the change in the contents of msg made in try2.js affect try value of msg in try1.js?

是的,确实如此。没有副本。 exports 对象是直接共享的。所有使用该模块的人都会看到您对该导出对象所做的任何更改。

仅供引用,模块可以使用 Object.freeze(module.exports) 来防止在添加所需属性后对该对象进行更改。

关于javascript - node js 中的 module.exports 是否创建导出对象或函数的浅拷贝或深拷贝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44475168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com