模块不更改其他文件的变量?
我有两个阵列的对象,我在server.js
的末尾带有module.exports
,将在另一个文件中用于显示分数,得分.js
。当我尝试重置新游戏时,我的问题就会发生,并且出于某种原因,某些数据似乎没有在scores.js
中更新,而其他数据则是其他数据。
我一直在研究,并认为这与JavaScript保存对象的方式有关,并且我一直在尝试不同的深/浅克隆方法,但是似乎没有任何作用。
更具体地说,在一个称为playerschoposing
的对象数组中有一个属性,它也是一个数组在server.js
文件中。
注意:这是我第一次使用node.js正确,因此肯定会缺少非常基本的东西。任何帮助都将受到赞赏!
这些是两个对象的示例:
Current player definitions [
{ definition: 'maiming; mutilation', playersChoosing: [ 'player4' ] },
{
definition: 'blah blah',
id: 'vIe_bxsTjg4OzFdJAAAB',
playersChoosing: [],
choseCorrect: false
},
{
definition: 'whatever',
id: 'b7lJloolPhLGdrrnAAAD',
playersChoosing: [ 'player1', 'player2', 'player3' ],
choseCorrect: true
},
{
definition: 'foo',
id: 'o7rTbhVp8WllpLTwAAAF',
playersChoosing: [],
choseCorrect: false
},
{
definition: 'bar',
id: 'lMnqOsIzdfQMBWPVAAAH',
playersChoosing: [],
choseCorrect: false
}
]
Game connections [
{
id: 'vIe_bxsTjg4OzFdJAAAB',
nickname: 'player1',
ready: true,
score: 2
},
{
id: 'b7lJloolPhLGdrrnAAAD',
nickname: 'player2',
ready: true,
score: 4
},
{
id: 'o7rTbhVp8WllpLTwAAAF',
nickname: 'player3',
ready: true,
score: 1
},
{
id: 'lMnqOsIzdfQMBWPVAAAH',
nickname: 'player4',
ready: true,
score: 1
}
]
I have two arrays of objects that I am exporting at the end of server.js
with module.exports
which will be used in another file to display scores, scores.js
. My problem occurs when I try and reset these arrays when a new games begins and for some reason some data seems to not update in scores.js
whilst others do.
I have been researching and think it has something to do with the way javascript saves objects and I have been trying different ways of deep/shallow cloning however nothing seems to work.
More specifically, there is a property in one of the arrays of objects called playersChoosing
which is also an array and is the one that seems not to change even if I completely set the array of objects to an empty one in the server.js
file.
Note: This is my first time properly using node.js so could definitely be missing something very basic. Any help is appreciated!
These are examples of the two objects:
Current player definitions [
{ definition: 'maiming; mutilation', playersChoosing: [ 'player4' ] },
{
definition: 'blah blah',
id: 'vIe_bxsTjg4OzFdJAAAB',
playersChoosing: [],
choseCorrect: false
},
{
definition: 'whatever',
id: 'b7lJloolPhLGdrrnAAAD',
playersChoosing: [ 'player1', 'player2', 'player3' ],
choseCorrect: true
},
{
definition: 'foo',
id: 'o7rTbhVp8WllpLTwAAAF',
playersChoosing: [],
choseCorrect: false
},
{
definition: 'bar',
id: 'lMnqOsIzdfQMBWPVAAAH',
playersChoosing: [],
choseCorrect: false
}
]
Game connections [
{
id: 'vIe_bxsTjg4OzFdJAAAB',
nickname: 'player1',
ready: true,
score: 2
},
{
id: 'b7lJloolPhLGdrrnAAAD',
nickname: 'player2',
ready: true,
score: 4
},
{
id: 'o7rTbhVp8WllpLTwAAAF',
nickname: 'player3',
ready: true,
score: 1
},
{
id: 'lMnqOsIzdfQMBWPVAAAH',
nickname: 'player4',
ready: true,
score: 1
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个人为的导出函数的示例,该函数管理在单例模块中而不是数据本身的数据。
然后是一个数据结构,例如 /code> 已经为您提供所有帮助者。 (
.clear
.get
.set
.entries
),该)将根据进行扩展
如何 您想保护模块数据很多Noreferrer“>不变的可能会方便地涵盖JS中处理共享数据的更多边缘案例以及该共享可能带来的意外修改。
例如,在初始数组代码中,即使
定义的副本
数组是从slice
返回数组中的对象,数组中的对象仍然引用相同的原始对象。模块设置之外的某些内容
defutions.1 ='argh'
由于对原始模块对象的引用,因此仍然修改共享对象。这在情况下可能很有用,或者在其他情况下痛苦。A contrived example of exporting functions that manage data housed in the singleton module, rather than the data itself.
Then there are data structures like a
Map
which already do all the helpers for you. (.clear
.get
.set
.entries
) that will act upon itselfWhich can be extended
Depending on how much you want to protect the modules data, something like Immutable might be handy to cover more of the edge cases of handling shared data in JS and the unexpected modifications that can come from that sharing.
For example, in the initial array code, Even though a copy of the
definitions
array is returned fromslice
the objects inside the array still reference the same original objects.Something outside the module setting
definitions.1 = 'argh'
still modifies the shared object due to that reference to the original modules object. That can be useful in cases, or a pain in others.让我们看一下此演示。我写三个.js文件类似:
好的,您运行
node main.js
;您发现值和P是相同的。
module.exports
只需导出一个对象,在main.js and Container.js中,我们共享该对象的参考values
。 >在container.js中,这些更改将在main.jsvalue
中看到。也许您未能共享参考,然后找不到更改。
let's take a look at this demo.I write three .js file something like:
Ok, you run
node main.js
;you find that values and p is same.
module.exports
just export an object, and in main.js and container.js, we share the reference of that objectvalues
.You changevalues
in container.js, and the changes will be seen in main.jsvalues
.Maybe you're failed to share the reference, and then you cannot find the changes.