模块不更改其他文件的变量?

发布于 2025-02-13 17:47:05 字数 1485 浏览 1 评论 0原文

我有两个阵列的对象,我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同展鸳鸯锦 2025-02-20 17:47:05

一个人为的导出函数的示例,该函数管理在单例模块中而不是数据本身的数据。

let definitions = [
  { 1: 'a' },
  { 2: 'b' },
  { 3: 'c' },
]

export function clearDefinitions(){
  definitions.length = 0
  return true
}

export function currentDefinitions(){
  return definitions // internal array
  return definitions.slice() // or copy of internal array
}

export function addDefinition(obj){
  const cloned_obj = JSON.parse(JSON.stringify(obj)) // copy of arg
  definitions.push(cloned_obj)
  return true
}

然后是一个数据结构,例如 /code> 已经为您提供所有帮助者。 (.clear .get .set .entries),该)将根据

export const definitions = new Map()

进行扩展

export class NedMap extends Map {
  gameTransform(obj) {
    // do things
    this.set(obj.id, obj)
    return this
  }
}

export const definitions = new NedMap()

如何 您想保护模块数据很多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.

let definitions = [
  { 1: 'a' },
  { 2: 'b' },
  { 3: 'c' },
]

export function clearDefinitions(){
  definitions.length = 0
  return true
}

export function currentDefinitions(){
  return definitions // internal array
  return definitions.slice() // or copy of internal array
}

export function addDefinition(obj){
  const cloned_obj = JSON.parse(JSON.stringify(obj)) // copy of arg
  definitions.push(cloned_obj)
  return true
}

Then there are data structures like a Map which already do all the helpers for you. (.clear .get .set .entries) that will act upon itself

export const definitions = new Map()

Which can be extended

export class NedMap extends Map {
  gameTransform(obj) {
    // do things
    this.set(obj.id, obj)
    return this
  }
}

export const definitions = new NedMap()

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 from slice 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.

霓裳挽歌倾城醉 2025-02-20 17:47:05

让我们看一下此演示。我写三个.js文件类似:

// values.js
module.exports = {
   name: 'jack',
   score: [34]
}
// container.js
const values = require('./values')
values.score = []
module.exports = {
   name: 'peter',
   container: values
}
// main.js
const values = require('./values')
const p = require('./container')
console.log('values: ', values)
console.log('p: ', p.container)

好的,您运行node main.js;
您发现值和P是相同的。
module.exports只需导出一个对象,在main.js and Container.js中,我们共享该对象的参考values。 >在container.js中,这些更改将在main.js value中看到。
也许您未能共享参考,然后找不到更改。

let's take a look at this demo.I write three .js file something like:

// values.js
module.exports = {
   name: 'jack',
   score: [34]
}
// container.js
const values = require('./values')
values.score = []
module.exports = {
   name: 'peter',
   container: values
}
// main.js
const values = require('./values')
const p = require('./container')
console.log('values: ', values)
console.log('p: ', p.container)

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 object values.You change values in container.js, and the changes will be seen in main.js values.
Maybe you're failed to share the reference, and then you cannot find the changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文