读书人

js object 转化作string对象

发布时间: 2012-12-14 10:33:07 作者: rapoo

js object 转化为string对象

?

/* object to string */function obj2str(o){var r = [], i, j = 0, len;if(o == null) {return o;}if(typeof o == 'string'){return '"'+o+'"';}if(typeof o == 'object'){if(!o.sort){r[j++]='{';for(i in o){r[j++]= '"';r[j++]= i;r[j++]= '":';r[j++]= obj2str(o[i]);r[j++]= ',';}//可能的空对象//r[r[j-1] == '{' ? j:j-1]='}';r[j-1] = '}';}else{r[j++]='[';for(i =0, len = o.length;i < len; ++i){r[j++] = obj2str(o[i]);r[j++] = ',';}//可能的空数组r[len==0 ? j:j-1]=']';}return r.join('');}return o.toString();}

?

?

推荐:android开发网

原文:http://blog.csdn.net/do_it__/article/details/6740303

?

读书人网 >JavaScript

热点推荐