[Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
2009-07-19 15:57
1221 查看
/********************************************************************************* * * Description : Functions for Input/Output * Copyright : Copyright(c) 2009 * Company : Be Technology Inc. * * @author Moky * @version 1.0 2009-03-28 * *********************************************************************************/ /** * Output all attributes of the object * * @created by moky, 2009-03-28 */ function print_r(obj) { //document.write(sprint_r(obj)); document.write(obj2xml(obj)); } //------------------------------------- /** * * @created by moky, 2009-11-10 */ function obj2xml(obj, level) { if (!level) level = 0; var brln = '<br/>/r/n'; var tab = ''; for (var x = 0; x < level; x++) tab += ' '; var ret = ''; //object, string, number, boolean, null, undefined if (typeof(obj) == 'object') { for (var k in obj) { var son = obj[k]; ret += tab; if (isNaN(k)) { ret += '<' + k + ' type="' + typeof(son) + '">'; ret += obj2xml(son, level+1); ret += '</' + k + '>'; } else { ret += '<<b>arrayItem</b> index="' + k + '" type="' + typeof(son) + '">'; ret += brln; ret += obj2xml(son, level+1); ret += '</<b>arrayItem</b>>'; } ret += brln; } } else if (typeof(obj) == 'string') { ret = '<![CDATA[' + obj + ']]>'; } else { ret = obj; } return ret; } /** * * @created by moky, 2009-03-28 */ function sprint_r(obj, level) { var ret = ''; if (!obj) { return ret; } var S = 0; if (!level) level = S; if (level <= S) { ret += '<pre style="border:1px solid #e0e0f5; padding:8px 5px; background-color:#fffff0; color:#7777aa; text-align:left; " mce_style="border:1px solid #e0e0f5; padding:8px 5px; background-color:#fffff0; color:#7777aa; text-align:left; ">'; } if (!obj) { ret += 'null'; ret += '/r/n'; return ret; } switch (typeof(obj)) { //"number", "string", "boolean", "object", "function" and "undefined" case 'number' : case 'boolean' : case 'undefined' : ret += obj; break; case 'string' : ret += '"' + obj + '"'; break; case 'function' : ret += '[function]'; break; case 'object' : if (Object.prototype.toString.call(obj) === '[object Array]') { ret += '['; } else { ret += '{'; } ret += '/r/n'; var bl = false; for (var name in obj) { if (bl) { ret += ',/n'; } else { bl = true; } ret += sprint_r_indent(level + 1); if (isNaN(name)) { ret += name + ':'; } var value = obj[name]; sprint_r(value, level + 1); } ret += '/r/n'; ret += sprint_r_indent(level); if (Object.prototype.toString.call(obj) === '[object Array]') { ret += ']'; } else { ret += '}'; } break; default : ret += 'unknown value: [' + value + ']'; } if (level <= S) { ret += '</pre>'; } return ret; } /** * * @created by moky, 2009-03-28 */ function sprint_r_indent(level, s) { var ret = ''; for (var i = 0; i < level; i++) { ret += '<font color="#f7f7f7">__</font>'; } if (s) { ret += s; } return ret; }
相关文章推荐
- The transaction log for database 'NJ_Address' is full. To find out why space in the log cannot be
- rapidxml 在mac下的编译error: call to function 'print_pi_node' that is neither visible in the template def
- The bundle does not contain an app icon for iPhone / iPod Touch of exactly '120x120' pixels, in .pn
- print 'welcome to masm!' in the center of the screen.
- no rule to process file 'xxx/.../jquery.js' of type sourcecode.javascript for architecture i386
- How to access the properties of an object in Javascript
- JS:Trim() in javascript, how to define a function of checkinput for a WebControl(ascx)
- 错误代码: 1582 Incorrect parameter count in the call to native function 'str_to_date'
- hive 终端产生的问题 (Failed to start database 'metastore_db', see the next exception for details.)
- 关于error:Cannot assign to 'self' outside of a method in the init family
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Font '宋体' is not available to the JVM. See the Javadoc for more details.
- PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone
- The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not refere
- Unable to find a value for "tStatus" in object of class org.entity.Passport using operator "."
- 关于error:Cannot assign to 'self' outside of a method in the init family
- 'Operation is not valid due to the current state of the object' error during postback
- The stack 'CSTACK' is filled to 100% (4096 bytes used out of 4096) 错误解决
- The bundle does not contain an app icon for iPhone / iPod Touch of exactly '57x57' pixels
- 关于error:Cannot assign to 'self' outside of a method in the init family