什么是java脚本代码的输出?

时间:2017-05-10 17:34:21

标签: javascript

 var a = {};
 b = {x:4};
 c = {y: 2};
a[b] = 123; 
a[c] = 456;

console.log(a[b]);

我在想,输出应该是123而不是,我不知道..

  

什么是输出并解释原因

1 个答案:

答案 0 :(得分:0)

The output will be 456
// It creates an empty object
var a = {};
b = {
  x: 4
}; // b become a new object
c = {
  y: 2
}; // c become a new object
/**Next line trying to create a key in object a which will look like {x:4}
which is invalid so it will look like {[object Object]} and its value will be 123**/
a[b] = 123;
/**Same operation with following line and its value will be 456.
In this case the previous[object Object] will be over written by
new one so both occupy the same key and hence 123 is overwritten 456**/
a[c] = 456;