JavaScript-Javascript中有没有类似PHP的print_r函数的方法?

UI设计界面 UI设计界面 主题:1059 回复:2190

JavaScript-Javascript中有没有类似PHP的print_r函数的方法?

浮生未歇 发布于 2017-01-01 字数 0 浏览 1297 回复 4

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

支持 Markdown 语法,需要帮助?

评论(4

偏爱自由 2017-08-05 4 楼

分享我在用的一个方案:

 function print_r(theObj) {
var retStr = '';
if (typeof theObj == 'object') {
retStr += '<div style="font-family:Tahoma; font-size:7pt;">';
for (var p in theObj) {
if (typeof theObj[p] == 'object') {
retStr += '<div><b>['+p+'] => ' + typeof(theObj) + '</b></div>';
retStr += '<div style="padding-left:25px;">' + print_r(theObj[p]) + '</div>';
} else {
retStr += '<div>['+p+'] => <b>' + theObj[p] + '</b></div>';
}
}
retStr += '</div>';
}
return retStr;
}

虐人心 2017-06-14 3 楼

openjs的print_r实现:

 function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j&lt;level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects 
    for(var item in arr) {
        var value = arr[item];

        if(typeof(value) == 'object') { //If it is an array,
            dumped_text += level_padding + "'" + item + "' ...n";
            dumped_text += dump(value,level+1);
        } else {
            dumped_text += level_padding + "'" + item + "' =&gt; "" + value + ""n";
        }
    }
} else { //Stings/Chars/Numbers etc.
    dumped_text = "===&gt;"+arr+"&lt;===("+typeof(arr)+")";
}
return dumped_text;

}

想挽留 2017-03-09 2 楼

你这个问题百度上有很多的,js没有现成的函数,需要自己封装,给你一个:

$(document).ready(function(){
$('#btn').click(function(){
var jsonStr = $('#jsonData').val();
var json = eval('('+jsonStr+')');
(function(){
var print_r = function(o, depth) {
var result = '';
depth || (depth=1);
var indent = new Array(4depth+1).join(' ');
var indentNext = new Array(4
(depth+1)+1).join(' ');
var indentNextTwo = new Array(4*(depth+2)+1).join(' ');
var tmp = '';
var type = typeof o;
switch(type) {
case 'string':
case 'number':
case 'boolean':
case 'undefined':
case 'function':
tmp += indent + indentNext + o + "n";
break;
case 'object':
default:
for(var key in o) {
tmp += indentNextTwo + '[' + key + '] = ';
tmp += print_r(o[key], (depth+1));
}
}
result += type + "n";
result += indentNext + '(' + "n";
result += tmp;
result += indentNext + ')' + "n";
return result;
};
alert(print_r(json));
}(json));
});
});

归属感 2017-02-15 1 楼

给你推荐个网站 phpjs,这里有php函数的js实现

phpjs

function print_r (array, return_val) {
// Prints out or returns information about the specified variable
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/print_r
// + original by: Michael White (http://getsprink.com)
// + improved by: Ben Bryan
// + input by: Brett Zamir (http://brett-zamir.me)
// + improved by: Brett Zamir (http://brett-zamir.me)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// - depends on: echo
// example 1: print_r(1, true);
//
returns 1: 1
var output = '',
pad_char = ' ',
pad_val = 4,
d = this.window.document,
getFuncName = function (fn) {
var name = (/Wfunctions+([w$]+)s(/).exec(fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
},
repeat_char = function (len, pad_char) {
var str = '';
for (var i = 0; i < len; i++) {
str += pad_char;
}
return str;
},
formatArray = function (obj, cur_depth, pad_val, pad_char) {
if (cur_depth > 0) {
cur_depth++;
}

        var base_pad = repeat_char(pad_val * cur_depth, pad_char);
        var thick_pad = repeat_char(pad_val * (cur_depth + 1), pad_char);
        var str = '';

        if (typeof obj === 'object' &amp;&amp; obj !== null &amp;&amp; obj.constructor &amp;&amp; getFuncName(obj.constructor) !== 'PHPJS_Resource') {
            str += 'Arrayn' + base_pad + '(n';
            for (var key in obj) {
                if (Object.prototype.toString.call(obj[key]) === '[object Array]') {
                    str += thick_pad + '[' + key + '] =&gt; ' + formatArray(obj[key], cur_depth + 1, pad_val, pad_char);
                }
                else {
                    str += thick_pad + '[' + key + '] =&gt; ' + obj[key] + 'n';
                }
            }
            str += base_pad + ')n';
        }
        else if (obj === null || obj === undefined) {
            str = '';
        }
        else { // for our "resource" class
            str = obj.toString();
        }

        return str;
    };

output = formatArray(array, 0, pad_val, pad_char);

if (return_val !== true) {
    if (d.body) {
        this.echo(output);
    }
    else {
        try {
            d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
            this.echo('&lt;pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;"&gt;' + output + '&lt;/pre&gt;');
        } catch (e) {
            this.echo(output); // Outputting as plain text may work in some plain XML
        }
    }
    return true;
}
return output;

}