您的位置:首页 > Web前端 > Node.js

用nodejs实现PHP的print_r函数代码

2014-03-14 00:00 1031 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: