Friday, 15 June 2012

javascript - Json-object gets injected into VAR object upward using push function -



javascript - Json-object gets injected into VAR object upward using push function -

i wonder how happens !

i have json array containing one value. assign var obj named 'first'. assign first's value other var obj called 'second'. then, force json value stored in 'third' 'second' obj push() fun. according knowledge, 'first' obj should have 'hello 1' value & 'second' obj should have ('hello 1' & 'hello 2' values. when check console log of browser, can see both values ('hello 1', 'hello 2') or 2 objects injected both var objects 'first' & 'second'.

function jsonarray() { var first=[{name:"hello 1"}] var second=first; var third=[{name:"hello 2"}] second.push(third); console.log(third); console.log(second); console.log(first); }

i don't know if wrong or out of knowledge. please update me appropriate explanation.

js fiddle: http://jsfiddle.net/micronyks/ellzw/

when assign first value sec var, assing reference, both variables point same object. if want differents objects every variable, need re-create have @ answer: http://stackoverflow.com/a/7486130/2873381

javascript

No comments:

Post a Comment