Javascript io-发出JS对象会导致最大递归错误,然后不会';我根本不发送
我有一个存储两个其他对象的对象。自上次加载页面以来,首次将此特定对象发送到服务器时,出现以下错误:Javascript io-发出JS对象会导致最大递归错误,然后不会';我根本不发送,javascript,websocket,socket.io,Javascript,Websocket,Socket.io,我有一个存储两个其他对象的对象。自上次加载页面以来,首次将此特定对象发送到服务器时,出现以下错误: Uncaught RangeError: Maximum call stack size exceeded at Function.[Symbol.hasInstance] (<anonymous>) at r (socket.io.js:1) at r (socket.io.js:1) at r (socket.io.js:1) at r (socket.io.js:1) at r
Uncaught RangeError: Maximum call stack size exceeded
at Function.[Symbol.hasInstance] (<anonymous>)
at r (socket.io.js:1)
at r (socket.io.js:1)
at r (socket.io.js:1)
at r (socket.io.js:1)
at r (socket.io.js:1)
at r (socket.io.js:1)
at r (socket.io.js:1)
at r (socket.io.js:1)
at r (socket.io.js:1)
未捕获范围错误:超过最大调用堆栈大小
在函数处。[Symbol.hasInstance](我发现了问题所在:只有在发送类实例时才会发生此错误。通过从一个实例提取数据并通过标准JavaScript对象文本发送数据,然后将数据重新应用到一个单独的实例才能解决问题。那么,对象是否具有循环引用(例如,作为属性的父/子/根关系?)你能描述一下你要发送的对象的结构吗?我用了“this.”好几次,但除此之外,我不认为在没有看到你要发送的对象的情况下,除了思考可能的原因之外,你的问题没有什么可以回答的。请提供关于你要发送的结构的更多信息
data {
p1: {
name: "Player 1",
obj: {
angle:0,
dir:0,
health:100,
name:"Player 1",
pos:p5.Vector {p5: p5, x: 0, y: 0, z: 0},
projectiles:[],
show: {
noStroke();
fill(255);
for (let i in this.projectiles) { this.projectiles[i].show() }
push();
translate(this.pos.x, this.pos.y);
rotate(PI - this.angle);
imageMode(CENTER);
image(pgraphic, 0, 0, this.health / 2, this.health / 2);
pop();
}
update: function () {
if (this.useControls) {
if (keyIsDown(68))
this.angle -= 0.125;
else if (keyIsDown(65))
this.angle += 0.125;
if (keyIsDown(87)) {
this.dir = 1;
} else if (keyIsDown(83)) {
this.dir = -1;
} else if (!keyIsDown(87) && !keyIsDown(83))
this.dir = 0;
if (keyIsDown(32))
this.shoot()
this.pos.x += ((this.health / 10) * sin(this.angle)) * this.dir;
this.pos.y += ((this.health / 10) * cos(this.angle)) * this.dir;
this.pos.x = constrain(this.pos.x, 0 - width / 2, width / 2);
this.pos.y = constrain(this.pos.y, 0 - height / 2, height / 2);
for (let i in this.projectiles) {
this.projectiles[i].update()
if (this.projectiles[i].pos.x <= 0 - (width / 2) ||
this.projectiles[i].pos.x >= (width / 2) ||
this.projectiles[i].pos.y <= 0 - ((height / 2) * 1.5) ||
this.projectiles[i].pos.y >= (height / 2)) {
this.projectiles.splice(i, 1)
}}}
}
useControls:true,
vel:p5.Vector {p5: p5, x: 0, y: 0, z: 0},
}
},
p2: {
name: "Player 2",
obj: {
angle:0,
dir:0,
health:100,
name:"Player 2",
pos:p5.Vector {p5: p5, x: 0, y: 0, z: 0},
projectiles:[],
show: {
noStroke();
fill(255);
for (let i in this.projectiles) { this.projectiles[i].show() }
push();
translate(this.pos.x, this.pos.y);
rotate(PI - this.angle);
imageMode(CENTER);
image(pgraphic, 0, 0, this.health / 2, this.health / 2);
pop();
}
update: function () {
if (this.useControls) {
if (keyIsDown(68))
this.angle -= 0.125;
else if (keyIsDown(65))
this.angle += 0.125;
if (keyIsDown(87)) {
this.dir = 1;
} else if (keyIsDown(83)) {
this.dir = -1;
} else if (!keyIsDown(87) && !keyIsDown(83))
this.dir = 0;
if (keyIsDown(32))
this.shoot()
this.pos.x += ((this.health / 10) * sin(this.angle)) * this.dir;
this.pos.y += ((this.health / 10) * cos(this.angle)) * this.dir;
this.pos.x = constrain(this.pos.x, 0 - width / 2, width / 2);
this.pos.y = constrain(this.pos.y, 0 - height / 2, height / 2);
for (let i in this.projectiles) {
this.projectiles[i].update()
if (this.projectiles[i].pos.x <= 0 - (width / 2) ||
this.projectiles[i].pos.x >= (width / 2) ||
this.projectiles[i].pos.y <= 0 - ((height / 2) * 1.5) ||
this.projectiles[i].pos.y >= (height / 2)) {
this.projectiles.splice(i, 1)
}}}
}
useControls:true,
vel:p5.Vector {p5: p5, x: 0, y: 0, z: 0},
}
}
}