JavaScript变量导出的行为出现了意外情况 – 为什么它在文件之间没有更新?

huangapple go评论100阅读模式
英文:

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.jslist没有被更新。我找到的一个修复方法是使用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.jstest.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(&quot;item&quot;);
  list = [&quot;item&quot;];
  console.log(&quot;B. list length &quot; + list.length);
}

const importedList = list; // mocking require(&quot;./index&quot;);

console.log(&quot;A. importedList length &quot; + importedList.length);
add();
console.log(&quot;C. importedList length &quot; + 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;
}

huangapple
  • 本文由 发表于 2023年8月9日 00:20:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定