COBOL-guy questions : Why is simple complex?
COBOL-guy questions : Why is simple complex?
var
test = {} ;
test.simple = {} ;
test.simple.par1 = 1;
test.complex = test.simple;
test.complex.par2 = 2;
alert( JSON.stringify(
test. simple));
And how to prevent?
]]>
< ![CDATA[
Simple is complex, because complex and simple contani the same reference. You have to copy the whole object. I don’t know how to do that best.
]]>
< ![CDATA[
https://www.google.com/search?q=javascript+clone+object
Yes, it’s a Google search link.
It seems that it depends on the complexity of the object you want to clone, and how fast you want it to be done.
For simple objects use
test.complex=JSON.parse(JSON.stringify(test.simple));
]]>
< ![CDATA[
hhhm, especially when in my original script test is ‘attached’ to self…..
]]>
< ![CDATA[
TrianguloY never would have Googled with clone, maybe expand. But thanks for your reply. Even complex is not too big to copy the code completely.
]]>