当前位置:  开发笔记 > 编程语言 > 正文

在Javascript中是否存在var_dump(PHP)的等价物?

如何解决《在Javascript中是否存在var_dump(PHP)的等价物?》经验,为你挑选了8个好方法。

我们需要查看对象在Javascript中有哪些方法/字段.



1> nickf..:

正如其他人所说,你可以使用Firebug,这样就不用担心Firefox了.Chrome和Safari都有一个内置的开发者控制台,它与Firebug的控制台具有几乎相同的界面,因此您的代码应该可以在这些浏览器中移植.对于其他浏览器,有Firebug Lite.

如果Firebug不适合你,那么试试这个简单的脚本:

function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }

    alert(out);

    // or, if you wanted to avoid alerts...

    var pre = document.createElement('pre');
    pre.innerHTML = out;
    document.body.appendChild(pre)
}

我建议不要警告每个属性:一些对象有很多属性,你会在那里整天点击"确定","确定","确定","我......该死的那是我的财产寻找".


我也建议反对它 - 坦率地说,我只是使用console.debug.但我指出了循环的可能性 - 这取决于用户他们想要对每个属性做什么

2> Ken..:

如果您使用的是firefox,则firebug插件控制台是检查对象的绝佳方式

console.debug(myObject);

或者,您可以循环遍历属性(包括方法),如下所示:

for (property in object) {
    // do what you want with property, object[property].value
}


`console.debug()`技巧也适用于Chrome.

3> 小智..:

许多现代浏览器都支持以下语法:

JSON.stringify(myVar);


它在接收圆形结构时触发异常,而不是防止它们.

4> 小智..:

不能说明你可以使用console.debug(object).如果你以此为生,这种技术每年可以节省数百小时:p


棒极了.我在今天之前从未听说过console.debug(对象),它给我节省了大量时间在我已经奋斗了三天的表格上.花了20分钟,我把它修好了.谢谢!

5> ReverseEMF..:

要从这个问题的标题的上下文回答这个问题,这里有一个类似于PHP var_dump的函数.它每次调用只转储一个变量,但它表示数据类型和值,它遍历数组和对象[即使它们是对象数组,反之亦然].我相信这可以改进.我更像是一个PHP家伙.

/**
 * Does a PHP var_dump'ish behavior.  It only dumps one variable per call.  The
 * first parameter is the variable, and the second parameter is an optional
 * name.  This can be the variable name [makes it easier to distinguish between
 * numerious calls to this function], but any string value can be passed.
 * 
 * @param mixed var_value - the variable to be dumped
 * @param string var_name - ideally the name of the variable, which will be used 
 *       to label the dump.  If this argumment is omitted, then the dump will
 *       display without a label.
 * @param boolean - annonymous third parameter. 
 *       On TRUE publishes the result to the DOM document body.
 *       On FALSE a string is returned.
 *       Default is TRUE.
 * @returns string|inserts Dom Object in the BODY element.
 */
function my_dump (var_value, var_name)
{
    // Check for a third argument and if one exists, capture it's value, else
    // default to TRUE.  When the third argument is true, this function
    // publishes the result to the document body, else, it outputs a string.
    // The third argument is intend for use by recursive calls within this
    // function, but there is no reason why it couldn't be used in other ways.
    var is_publish_to_body = typeof arguments[2] === 'undefined' ? true:arguments[2];

    // Check for a fourth argument and if one exists, add three to it and
    // use it to indent the out block by that many characters.  This argument is
    // not intended to be used by any other than the recursive call.
    var indent_by = typeof arguments[3] === 'undefined' ? 0:arguments[3]+3;

    var do_boolean = function (v)
    {
        return 'Boolean(1) '+(v?'TRUE':'FALSE');
    };

    var do_number = function(v)
    {
        var num_digits = (''+v).length;
        return 'Number('+num_digits+') '+v;
    };

    var do_string = function(v)
    {
        var num_chars = v.length;
        return 'String('+num_chars+') "'+v+'"';
    };

    var do_object = function(v)
    {
        if (v === null)
        {
            return "NULL(0)";
        }

        var out = '';
        var num_elem = 0;
        var indent = '';

        if (v instanceof Array)
        {
            num_elem = v.length;
            for (var d=0; d 0 ? var_name + ' = ':''; // Turns labeling on if var_name present, else no label
            out += v_name + do_boolean(var_value);
            break;
        case "number":
            v_name = var_name.length > 0 ? var_name + ' = ':'';
            out += v_name + do_number(var_value);
            break;
        case "string":
            v_name = var_name.length > 0 ? var_name + ' = ':'';
            out += v_name + do_string(var_value);
            break;
        case "object":
            v_name = var_name.length > 0 ? var_name + ' => ':'';
            out += v_name + do_object(var_value);
            break;
        case "function":
            v_name = var_name.length > 0 ? var_name + ' = ':'';
            out += v_name + "Function";
            break;
        case "undefined":
            v_name = var_name.length > 0 ? var_name + ' = ':'';
            out += v_name + "Undefined";
            break;
        default:
            out += v_name + ' is unknown type!';
    }

    // Using indent_by to filter out recursive calls, so this only happens on the 
    // primary call [i.e. at the end of the algorithm]
    if (is_publish_to_body  &&  indent_by === 0)
    {
        var div_dump = document.getElementById('div_dump');
        if (!div_dump)
        {
            div_dump = document.createElement('div');
            div_dump.id = 'div_dump';

            var style_dump = document.getElementsByTagName("style")[0];
            if (!style_dump)
            {
                var head = document.getElementsByTagName("head")[0];
                style_dump = document.createElement("style");
                head.appendChild(style_dump);
            }
            // Thank you Tim Down [http://stackoverflow.com/users/96100/tim-down] 
            // for the following addRule function
            var addRule;
            if (typeof document.styleSheets != "undefined" && document.styleSheets) {
                addRule = function(selector, rule) {
                    var styleSheets = document.styleSheets, styleSheet;
                    if (styleSheets && styleSheets.length) {
                        styleSheet = styleSheets[styleSheets.length - 1];
                        if (styleSheet.addRule) {
                            styleSheet.addRule(selector, rule)
                        } else if (typeof styleSheet.cssText == "string") {
                            styleSheet.cssText = selector + " {" + rule + "}";
                        } else if (styleSheet.insertRule && styleSheet.cssRules) {
                            styleSheet.insertRule(selector + " {" + rule + "}", styleSheet.cssRules.length);
                        }
                    }
                };
            } else {
                addRule = function(selector, rule, el, doc) {
                    el.appendChild(doc.createTextNode(selector + " {" + rule + "}"));
                };
            }

            // Ensure the dump text will be visible under all conditions [i.e. always
            // black text against a white background].
            addRule('#div_dump', 'background-color:white', style_dump, document);
            addRule('#div_dump', 'color:black', style_dump, document);
            addRule('#div_dump', 'padding:15px', style_dump, document);

            style_dump = null;
        }

        var pre_dump = document.getElementById('pre_dump');
        if (!pre_dump)
        {
            pre_dump = document.createElement('pre');
            pre_dump.id = 'pre_dump';
            pre_dump.innerHTML = out+"\n";
            div_dump.appendChild(pre_dump);
            document.body.appendChild(div_dump);
        }  
        else
        {
            pre_dump.innerHTML += out+"\n";
        }
    }
    else
    {
        return out;
    }
}



6> David..:

在firebug或google-chrome web-inspector中的console.dir(朝向链接页面的底部)将输出对象属性的交互式列表.

另见Stack-O答案



7> Paul Dixon..:

如果使用Firebug,则可以使用console.log输出对象并在控制台中获取超链接,可探索的项目.



8> 小智..:

您希望以JSON形式查看整个对象(其中包含对象和变量的所有嵌套级别).JSON代表JavaScript Object Notation,打印出对象的JSON字符串是一个很好的等价物var_dump(获取JavaScript对象的字符串表示).幸运的是,JSON在代码中非常容易使用,而JSON数据格式也非常易读.

例:

var objectInStringFormat = JSON.stringify(someObject);
alert(objectInStringFormat);

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有