View object content in JavaScript

Source: Internet
Author: User

JavaScript is a programming language based on objects. outputting the internal structure of JavaScript objects makes debugging easier. The following describes how to view the internal structure of an object so that you can view the attributes and values of the object at a glance.

Let's take a look at the simple method:

function dump_obj(myObject) {  var s = "";  for (var property in myObject) {  s = s + "n "+property +": " + myObject[property];  }    alert(s);  }  var obj = {name:'Gonn', class:'NowaMagic.net'};

The following method is more detailed:

var MAX_DUMP_DEPTH = 10;  function dumpObj(obj, name, indent, depth) {  if (depth > MAX_DUMP_DEPTH) {      return indent + name + ": <Maximum Depth Reached>n";    }    if (typeof obj == "object") {      var child = null;      var output = indent + name + "n";      indent += "t";      for (var item in obj) {        try {          child = obj[item];        } catch (e) {          child = "<Unable to Evaluate>";        }        if (typeof child == "object") {          output += dumpObj(child, item, indent, depth + 1);        } else {          output += indent + item + ": " + child + "n";        }  }  return output;    } else {      return obj;    }  }  var myObject = {name: "Jack B. Nimble", 'goto': 'Jail', grade: 'A', level: 3};//alert( dumpObj(myObject) );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.