// Recursive print of objectvar print = function( o, maxLevel, level ) {if ( typeof level == "undefined" ) {level = 0;}if ( typeof level == "undefined" ) {maxLevel = 0;}
var str = '';// Remove this if you don't want the pre tag, but make sure to remove// the close pre tag on the bottom as wellif ( level == 0 ) {str = '<pre>';}
var levelStr = '';for ( var x = 0; x < level; x++ ) {levelStr += ' ';}
if ( maxLevel != 0 && level >= maxLevel ) {str += levelStr + '...</br>';return str;}
for ( var p in o ) {if ( typeof o[p] == 'string' ) {str += levelStr +p + ': ' + o[p] + ' </br>';} else {str += levelStr +p + ': { </br>' + print( o[p], maxLevel, level + 1 ) + levelStr + '}</br>';}}
// Remove this if you don't want the pre tag, but make sure to remove// the open pre tag on the top as wellif ( level == 0 ) {str += '</pre>';}return str;};
用法:
var pagewilsObject = {name: 'Wilson Page',contact: {email: 'wilson@hotmail.com',tel: '123456789'}}
// Recursive of whole object$('body').append( print(pagewilsObject) );
// Recursive of myObject up to 1 level, will only show name// and that there is a contact object$('body').append( print(pagewilsObject, 1) );
注意:尝试打印document.body是一个糟糕的例子。首先,FF在使用toSource时只打印一个空的对象字符串。当使用上面的函数时,FF在SecurityError: The operation is insecure.上崩溃。Chrome将在Uncaught RangeError: Maximum call stack size exceeded上崩溃。显然,document.body不是要转换为字符串。因为它太大了,或者违反了访问某些属性的安全策略。除非,我在这里搞砸了什么,一定要告诉!
var print = function( o, maxLevel, level ){if ( typeof level == "undefined" ){level = 0;}if ( typeof maxlevel == "undefined" ){maxLevel = 0;}
var str = '';// Remove this if you don't want the pre tag, but make sure to remove// the close pre tag on the bottom as wellif ( level == 0 ){str = '<pre>'; // can also be <pre>}
var levelStr = '<br>';for ( var x = 0; x < level; x++ ){levelStr += ' '; // all those spaces only work with <pre>}
if ( maxLevel != 0 && level >= maxLevel ){str += levelStr + '...<br>';return str;}
for ( var p in o ){switch(typeof o[p]){case 'string':case 'number': // .tostring() gets automatically appliedcase 'boolean': // dittostr += levelStr + p + ': ' + o[p] + ' <br>';break;
case 'object': // this is where we become recursivedefault:str += levelStr + p + ': [ <br>' + print( o[p], maxLevel, level + 1 ) + levelStr + ']</br>';break;}}
// Remove this if you don't want the pre tag, but make sure to remove// the open pre tag on the top as wellif ( level == 0 ){str += '</pre>'; // also can be </pre>}return str;};
/*** @param variable mixed The var to log to the console* @param varName string Optional, will appear as a label before the var*/function dd(variable, varName) {var varNameOutput;
varName = varName || '';varNameOutput = varName ? varName + ':' : '';
console.warn(varNameOutput, variable, ' (' + (typeof variable) + ')');}
用法
dd(123.55);输出:
var obj = {field1: 'xyz', field2: 2016};dd(obj, 'My Cool Obj');
var gandalf = {"real name": "Gandalf","age (est)": 11000,"race": "Maia","haveRetirementPlan": true,"aliases": ["Greyhame","Stormcrow","Mithrandir","Gandalf the Grey","Gandalf the White"]};//to console log object, we cannot use console.log("Object gandalf: " + gandalf);console.log("Object gandalf: ");//this will show object gandalf ONLY in Google Chrome NOT in IEconsole.log(gandalf);//this will show object gandalf IN ALL BROWSERS!console.log(JSON.stringify(gandalf));//this will show object gandalf IN ALL BROWSERS! with beautiful indentconsole.log(JSON.stringify(gandalf, null, 4));
console.log("bwib:", bwib, "bwab:", bwab, "bwob": bwob) // old way Aconsole.log({bwib: bwib, bwab: bwab, bwob: bwob}) // old way B
console.log({bwib, bwab, bwob}) // ES2015+ way