0

This is the javascript code.

var option={
  value:'',
  desc:{
   en:'',
   hn:''
  }
};
option.value='15';
option.desc.en='hello';
option.desc.hn='world'; 

console.log(option);

option.value='25';
option.desc.hn='India';

console.log(option);

This gives the output-

object {value:25,object {desc:{en:"hello",hn:"India"}}}

object {value:25,object {desc:{en:"hello",hn:"India"}}}

Instead of

object {value:15,object {desc:{en:"hello",hn:"world"}}}

object {value:25,object {desc:{en:"hello",hn:"India"}}}

u_mulder
  • 54,101
  • 5
  • 48
  • 64
Nitish Kumar
  • 101
  • 6

3 Answers3

0

the output diff with different browser. I got the object {value:15,object {desc:{en:"hello",hn:"world"}}} object {value:25,object {desc:{en:"hello",hn:"India"}}} through chrome.

0

Like @Chales Tuang mentioned, I got the expected output on

jsfiddle

var option={
  value:'',
  desc:{
   en:'',
   hn:''
 }
};
option.value='15';
option.desc.en='hello';
option.desc.hn='world'; 

console.log(option);

option.value='25';
option.desc.hn='India';

console.log(option); 

The only explanation I could possibly think of for the case where you get same output in each call to console.log, is that the browser is applying the principle that objects are live in Javascript, as shown by Stoyan Stefanov in his book Object Oriented Javascript

As a result, the when you call console.log, it sees the latest, up-to-date property(ies) of the object, even though the object was augmented after the call, in as much as the update is within the scope of the call, it just grabs the current updated state of the object.

That's my thought on this, anyway.

NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
0

This is because of a bug in WebKit browsers (https://code.google.com/p/chromium/issues/detail?id=44720)

Instead of logging the entire object,try to log the particular value, you will get the expected result..

 console.log(option.desc.hn);

Or try JSON.stringify

 console.log(JSON.stringify(option));

JS Fiddle link : https://jsfiddle.net/t744ohog/2/