node.js - Passing/Comparing by reference for javascript objects -
i'm trying compare 2 objects. use socket.io send nodes
object client.
my server.js
goes:
var nodes = {'101':{id:'101',x:100,y:200}, '102':{id:'102',x:200,y:200}}; socket.emit('message', nodes);
in client, i'd create copy of object nodes
first time receives. save variable oldnodes
, next time socket receives nodes
object, comparison oldnodes
, output property has been changed.
e.g., if receives var nodes = {'101':{id:'101',x:100,y:200},'102':{id:'102',x:200,y:300}};
, should print(console.log) nodes[102].y changed
.
client.html
:
socket.on('message', function(nodes){ if (oldnodes == undefined){ var oldnodes = nodes; } else{ //compare oldnodes & nodes //print result } });
for copying, var oldnodes = nodes;
not want since javascript passes objects reference. so, if oldnodes & nodes
have same content, (oldnodes == nodes)
produce false
. how copy object , compare properties efficiently?
from code seems problem not passbyref related. if declare oldnodes outside of function , assign var inside if statement, oldnodes not defined in else block. might looking for?
if (oldnodes == undefined){ oldnodes = nodes; }
Comments
Post a Comment