javascrip实现PHPprint_r函数的数量(三种方法)
方法1复制代码代码如下所示:
功能print_r(theobj){
无功retstr =;
如果(typeof theobj = = 'object){
retstr + =;
对于(VAR P theobj){
如果(typeof theobj { p } = = 'object){
retstr ={+ P + } = > +类型+(theobj);
retstr + = + +(P print_r theobj { });
{人}
retstr ={+ P + P + theobj } { } += >;
}
}
retstr + =;
}
返回retstr;
}
方法二
复制代码代码如下所示:
$(文档)Ready(函数(){)
$(# BTN)。Click(function(){)
jsonstr = $(var的# JSONData)。瓦迩();
var json = eval('(' + jsonstr + ')');
(函数(){())
无功print_r =功能(O,深度){
var =;
| |深度(深度= 1);
var =新数组(4 *深度+ 1);
无功indentnext =新的阵列(4×(深度+ 1)+ 1)。加入();
无功indentnexttwo =新的阵列(4×(深度+ 2)+ 1)。加入();
var =;
变量类型=型O;
开关(类型){
case'string:
case'number:
case'boolean:
case'undefined:
case'function:
TMP =缩进+ indentnext + O +;
打破;
case'object:
违约:
对于(var在o中){
TMP = indentnexttwo +{+键+=};
TMP = print_r(O {key}(深度+ 1));
}
}
结果=类型+;
结果:indentnext(+;
结果= TMP;
结果:indentnext ++;
返回结果;
};
警报(print_r(JSON));
}(JSON);
});
});
方法三
复制代码代码如下所示:
print_r:功能(theobj){
无功retstr =;
如果(typeof theobj = 'object| |型theobj = = 'array){
retstr + =;
对于(VAR P theobj){
如果(typeof theobj { } { } theobj 'object'typeof P P | | = = = = 'array){
retstr ={+ P + } = > +类型+(theobj);
retstr + = + +(P xfupload.tools.print_r theobj { });
{人}
retstr ={+ P + P + theobj } { } += >;
}
}
retstr + =;
}
$(体)追加(retstr);
}