日期:2014-05-16 浏览次数:20296 次
var a = { init: function(){ return 'a'; } }, b = a; if(b['load'] == undefined) b['load'] = {}; b = b['load']; console.log(a);
var a = { init: function(){ return 'a'; } }, //上面定义了一个a b = a;//将a赋予b。 //你想明白的就是下面这句了 //b['load'] == undefined如果b中不存在load,那么b['load']={};你上面的是建了一个空对象。我这里加多了个属性a:'1' if(b['load'] == undefined) b['load'] = {a:'1'}; b = b['load'];//又把b['load']指回b了 alert(b.a);//因为b['load'] = {a:'1'};所以,这里弹出'1'
------解决方案--------------------
<script type="text/javascript"> var a = {}; var b = a;//相互鬼上身附体 a['c']={}; console.log(b); //Object { c={...}} b['a']={}; console.log(a); //Object { c={...}, a={...}} </script>