like function for javascript objects?

While debugging and developing with javascript, I often wanted to alert the objects, so I used the below code:

for(a in obj)
{
  alert(a +' = '+obj[a])
}    

It serves well, but it is too annoying. I want to know if there is anything just like this for arrays:

var temp = ['a','b','c'];
alert(temp); // it will alert a,b,c 

So what I wish to do is:

var temp = {a:'a',b:'b',c:'c'};
alert(temp) ; // It should alert json {a:'a',b:'b',c:'c'}

Or any other better suggestion so I could look up the object easily.


您也可以这样做,以便提醒的格式可以随心所欲

 Object.prototype.toString = function{
    var str='';
    for(a in this)
    {
      str+=a +' = '+obj[a]);
    }    
    }

Alert calls toString, so you can overwrite toString for debugging purposes:

Object.prototype.toString = function() {
    return JSON.stringify(this);
};

So you can just call alert(foo); and it will display the JSON representation of foo


Use

alert(JSON.stringify(temp)) ;

instead of

alert(temp) ;  
链接地址: http://www.djcxy.com/p/48210.html

上一篇: 如何在单独的行上记录每条JSON记录?

下一篇: 喜欢JavaScript对象的函数?