开发者

Learn about a javascript object

开发者 https://www.devze.com 2023-03-17 12:40 出处:网络
I have an [object Object] being passed as an argument through a jQuery function and I want to learn more about it.I don\'t want to know anything about this specific situation (unl开发者_如何学运维ess

I have an [object Object] being passed as an argument through a jQuery function and I want to learn more about it. I don't want to know anything about this specific situation (unl开发者_如何学运维ess there is no solution) but how to replicate something like php's var_dump().


You could use console.log(your_object_instance); and look in the FireBug console in FireFox. This also works with Chrome/Safari developers tools. You will learn everything you need about this object such as properties, methods, ...


function odump(object, depth, max){
  depth = depth || 0;
  max = max || 2;

  if (depth > max)
    return false;

  var indent = "";
  for (var i = 0; i < depth; i++)
    indent += "  ";

  var output = "";  
  for (var key in object){
    output += "\n" + indent + key + ": ";
    switch (typeof object[key]){
      case "object": output += odump(object[key], depth + 1, max); break;
      case "function": output += "function"; break;
      default: output += object[key]; break;        
    }
  }
  return output;
}


If you're using Firefox you can use Firebug, or if you're on Safari or Google Chrome you can use their developer tools / console. Then use the console.log() method to dump information about the object into the console:

console.log(obj);

or

console.log({test: "lalala"});
0

精彩评论

暂无评论...
验证码 换一张
取 消