我这里有两个文件:
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
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.