您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

用nodejs实现PHP的print

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 这篇文章主要介绍了用nodejs实现PHP的print_r函数代码,需要的朋友可以参考下

 代码如下: function ergodic(obj,indentation){   var indent = "  " + indentation;   if(obj.constructor == Array || obj.constructor == Object){       for(var p in obj){       if(obj[p].constructor == Array|| obj[p].constructor == Object){         console.log(indent + "["+p+"] => "+typeof(obj)+"");         console.log(indent + "{");         ergodic(obj[p], indent);         console.log(indent + "}");       } else if (obj[p].constructor == String) {         console.log(indent + "["+p+"] => '"+obj[p]+"'");       } else {         console.log(indent + "["+p+"] => "+obj[p]+"");       }     }   } }   function print_r(obj) {   console.log("{")   ergodic(obj, "");   console.log("}") }   var stu = {'name':'Alan','grade':{'Chinese':120,'math':130,'competition':{'NOI':'First prize'}}};   print_r(stu);