Javascript 数组在方法范围内消失

Javascript 数组在方法范围内消失,javascript,arrays,reference,instance,Javascript,Arrays,Reference,Instance,我正试图为我的javascript游戏创建一个类来添加多人游戏,但是在这个类中,我遇到了数组值变化的问题,正如您在sendNetEntities()函数中看到的那样 class NET\u IO{ //可能把地址放在这里 //我想检查localhost是否表示主客户端 构造函数(主机、网络层){ this.socket=io(); this.netLayer=netLayer console.log(this.socket) this.netEntities=this.netLayer.enti

我正试图为我的javascript游戏创建一个类来添加多人游戏,但是在这个类中,我遇到了数组值变化的问题,正如您在
sendNetEntities()
函数中看到的那样

class NET\u IO{
//可能把地址放在这里
//我想检查localhost是否表示主客户端
构造函数(主机、网络层){
this.socket=io();
this.netLayer=netLayer
console.log(this.socket)
this.netEntities=this.netLayer.entities
//setInterval(()=>{this.update()},200)
}
getNetEntities(){
this.socket.emit('getNetEntities',(net_entities)=>{
console.log(网络实体)
this.netEntities=net\u实体
})
}
sendNetEntities(层){
var netEnt=this.netEntities
log(netEnt)//返回[background:Entity,NIkTag:Entity,player:Entity]`我想要的值
var ent=JSON.stringify(netEnt);
console.log(ent)//返回[]
this.socket.emit('sendNetEntities',ent)
}
更新(层,回调){
//如果本地主机不检索新数据,请检查主机仅发送
此.sendNetEntities(层)
回调(this.netEntities)
}
}
我想我在变量作为引用而不是实例时遇到了问题。但我并不完全确定javascript背后的所有规则。谁能帮我解释一下这个问题吗。我愿意根据需要编辑我的问题

编辑

进一步的调试使我相信socket.io一定有某种问题。如果我运行这个
this.socket.emit('sendNetEntities',{netEnt})
我在服务器上的返回值是
{netEnt:[]}
我过去在socket.io中从未遇到过这样的问题。我做错什么了吗。socket.io是问题的根源吗

//this returns [background: Entity, NIkTag:  Entity, player: Entity]` the value i want
console.log(netEnt)                
var ent = JSON.stringify(netEnt);
console.log(ent)                   //this returns []

我认为您将
数组
视为
对象
。在JavaScript中,这在技术上是可能的,因为几乎所有东西都是对象,包括数组。但是,这可能会导致意外行为:

// Create an array and an object
a = [] // an array
o = {} // an object

// Set some properties on both
a.p = 42
o.p = 42

// Show differences between arrays and objects:
console.log(a.constructor)     // ƒ Array()
console.log(a)                 // [p: 42]
console.log(JSON.stringify(a)) // []

console.log(o.constructor)     // ƒ Object()
console.log(o)                 // {p: 42}
console.log(JSON.stringify(o)) // {"p":42}
如您所见,JSON.stringify()忽略数组上设置的属性

因此,解决方案是将
netEnt
用作数组或对象,而不混合以下类型:

// As an array, don't use property names. Use the integer array indices:
netEnt = [entity1, entity2, entity3]
background = netEnt[0]
nikTag     = netEnt[1]
player     = netEnt[2]    

// As an object, property names can be used:
netEnt = {background: entity1, NIkTag: entity2, player: entity3}
background = netEnt.background 
nikTag     = netEnt.NIkTag     
player     = netEnt.player

更新:

基本问题是类使用数组,但作为对象访问它们。最好的解决方案是更改类,使它们:

  • 使用阵列并以阵列的形式访问阵列
  • 使用对象并将对象作为对象访问
如果没有看到您的类定义,我无法向您演示如何执行此操作。但是,它非常简单,只需将类实例的初始值从
[]
更改为
{}

以下是将数组“对象”序列化为真正的JS对象的快速修复方法,因此JSON.stringify()将按预期工作。但是,在将来,我强烈建议学习JS数组和对象之间的区别。此快速修复方案会带来完全不必要的性能损失,因为JS数组被误用为对象:

    sendNetEntities(layer){
        var netEnt = this.netEntities
        
        // Convert netEnt array "object" into true JS object
        var trueObject = {}
        for (prop in netEnt) {
            trueObject[prop] = netEnt[prop]
        }

        var ent = JSON.stringify(trueObject);
        this.socket.emit('sendNetEntities', ent)
    }

注意:在
getNetEntities()
中,您可能需要执行相反的操作:将真正的JS对象转换回数组“objects”。我不确定
net_entities
的输入格式,因此我将此作为一个练习。

您应该在JSON.parse之前控制台.log(str)以查看负载是什么,然后当您获得负载时,放置在json解析器查看器中,以验证数据的真实性。Javascript是单线程的,不会在执行行之间更改。我的目标不一定是处理对象。我只是想把包含类对象的数组放到我的服务器上,然后再拿回来。我想坚持使用数组,因为这是我所有其他类存储实体的地方。@NikHendricks:你的类使用数组而不是对象有什么原因吗?问题在于,类将数据存储在数组中,但访问这些数组时却将其视为对象。将类更改为使用适当的对象而不是数组应该非常简单。如果您向我展示一个类定义,我可以向您展示所需的简单更改。@NikHendricks:我用一个快速修复程序更新了我的答案,
sendNetEntities()
,它将允许您将数组添加到服务器。(从服务器上取回它们需要类似的快速修复,但我无法重写
getNetEntities()
而不知道
netu entities
的确切内容)哦!明白。因此,从技术上讲,我的实体是数组中的对象。我不是真的在摆弄数组。我想我只是错过了你的解释谢谢!