英文:
Javascript variable export unexpected behavior - why doesn't it update across files?
问题
我这里有两个文件:
index.js
let list = [];
function add() {
//list.push("item");
list = ["item"];
console.log("B. list length " + list.length);
}
module.exports = {
add,
list
}
test.js
let { add, list } = require('./index');
console.log("A. list length " + list.length);
add();
console.log("C. list length " + list.length);
当前输出:
A. list length 0
B. list length 1
C. list length 0
期望输出:
A. list length 0
B. list length 1
C. list length 1
我不明白为什么在test.js中list
没有被更新。我找到的一个修复方法是使用list.push("item");
而不是list = ["item"];
。我不知道为什么会发生这种情况。
英文:
I have two files here:
index.js
let list = [];
function add() {
//list.push("item");
list = ["item"];
console.log("B. list length " + list.length);
}
module.exports = {
add,
list
}
test.js
let { add, list } = require('./index');
console.log("A. list length " + list.length);
add();
console.log("C. list length " + list.length);
Current output:
A. list length 0
B. list length 1
C. list length 0
Expected output:
A. list length 0
B. list length 1
C. list length 1
I don't understand why list
not being updated in test.js.
The one fix I found was to use list.push("item");
instead of list = ["item"];
.
I have no idea why this is happening.
答案1
得分: 1
当从另一个模块导入一个数组(或对象等)时,实际上是创建了对该数组的引用。在你的index.js
文件中使用list = ["item"];
重新分配list
,而.push
修改了index.js
中的list
,也修改了你在test.js
中导入的列表引用。重新分配会打破index.js
和test.js
之间的引用关系。index.js
仍然引用原始的空数组,而test.js
现在引用了新的数组。
let list = [];
function add() {
// list.push("item");
list = ["item"];
console.log("B. list length " + list.length);
}
const importedList = list; // 模拟 require("./index");
console.log("A. importedList length " + importedList.length);
add();
console.log("C. importedList length " + importedList.length);
英文:
When importing an array (or object, ...) from another module, you're actually creating a reference to that array. Using list = ["item"];
in your index.js
file you reassign list
whilst .push
modifies the list
in index.js
and also the list reference you import in test.js
. Reassignment breaks the reference between index.js
and test.js
. index.js
still refers to the original empty array, while test.js
now has a reference to the new array
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let list = [];
function add() {
// list.push("item");
list = ["item"];
console.log("B. list length " + list.length);
}
const importedList = list; // mocking require("./index");
console.log("A. importedList length " + importedList.length);
add();
console.log("C. importedList length " + importedList.length);
<!-- end snippet -->
答案2
得分: 0
你的代码将一个全新的数组赋值给了list
变量。然而,导出的值是对原始数组的引用。在"index"模块中更新局部变量并不会改变"test"模块中的变量值,换句话说。
这个问题与模块之间的导入和导出无关。这里的情况是一样的:
let a = [1, 2, 3];
let b = a;
a = [4, 5, 6];
console.log(b); // [1, 2, 3]
让变量a
引用一个新的数组不会影响变量b
的值。这是 JavaScript 工作方式的基本特性。
英文:
Your code assigns a completely new array to the value of list
. However, the exported value was a reference to the original array. Updating the local variable in the "index" module does not change the value of the variable in the "test" module, in other words.
The problem really has nothing to do with import and export across module boundaries. It is the same situation here:
let a = [1, 2, 3];
let b = a;
a = [4, 5, 6];
console.log(b); // [1, 2, 3]
Making the variable a
reference a new array does not affect the value of variable b
. That's a basic feature of how JavaScript works.
答案3
得分: -1
为了实现预期的行为,即在test.js中正确更新列表长度,您应该使用push方法或其他修改现有数组的方法,而不是分配一个新数组。以下是您可以这样做的方法:
// test.js
const list = [];
export function addItemToList(item) {
list.push(item); // 将项目添加到现有数组中
}
export function getListLength() {
return list.length;
}
英文:
To achieve the expected behavior, where the list length is updated correctly in test.js, you should use the push method or another method that modifies the existing array, instead of assigning a new array. Here's how you can do it:
// test.js
const list = [];
export function addItemToList(item) {
list.push(item); // Add the item to the existing array
}
export function getListLength() {
return list.length;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论