javascript通过对象递归 [英] javascript recursive through an object

查看:40
本文介绍了javascript通过对象递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于调试和日志记录的原因,我想递归地遍历一个对象.但我的实际代码片段在第一个对象之后中断.

I want to step recursively through an object for debugging and logging reasons. But my actual code snippet breaks after the first object.

function showData(obj)
{
    for(var key in eval(obj))
    {
        debugWriteLine('object = '+obj+' | property = '+key+' | type = '+typeof(eval(obj+'.'+key))+' | value = '+eval(obj+'.'+key));                    

        if(typeof(eval(obj+'.'+key))=== 'object')
        {
            obj= obj+'.'+key;           
            return showData(obj);
        }
    }
}

showData('$');

这仅显示了 $ 和 $.fn 的内容,但我需要 $ 的所有属性.

This showed me only the content of $ and $.fn but I need all the properties of $.

谢谢弗洛里安

推荐答案

第一:您既不需要也不想要 eval 来实现这些.从 $(对象)开始,然后在查看其属性之一时使用 obj[key].当该属性是非函数对象时递归.

First: You neither need nor want eval for any of this. Start with $ (the object) and then use obj[key] when you are looking into one of its properties. Recurse when that property is a non-function object.

您没有看到所有属性的原因是 for-in 循环仅遍历 enumerable 属性,而 jQuery 的许多属性是不可枚举的.您可以使用 getOwnPropertyNames 来获取对象的所有字符串键属性的名称,甚至是不可枚举的.您可以使用 getPrototypeOf 获取该对象的原型,以便枚举其属性(for-in 可以这样做).

The reason you're not seeing all of the properties is that for-in loops only loop through enumerable properties, and many of jQuery's properties are not enumerable. You can use getOwnPropertyNames to get the names of all string-keyed properties of an object, even non-enumerable ones. And you can use getPrototypeOf to get that object's prototype so you can enumerate its properties (which for-in does).

所以:

function showData(name, obj)
{
    while (obj && obj != Object.prototype)
    {
        // Get all of the object's property names, even non-enumerable ones
        var keys = Object.getOwnPropertyNames(obj);
        keys.forEach(function(key)
        {
            // We should restrict this check to function objects; left
            // as an exercise for the reader...
            if (key !== "caller" &&
                key !== "callee" &&
                key !== "arguments"
                )
            {
                var value = obj[key];
                var type = typeof value;
                debugWriteLine('object = ' + name + ' | property = ' + key + ' | type = ' + type + ' | value = ' + value); 

                if (type === 'object')
                {
                    return showData(name + "." + key, value);
                }
            }
        });

        // Get the object's prototype
        obj = Object.getPrototypeOf(obj);
    }
}

现场示例:

var debugWriteLine = console.log.bind(console);
function showData(name, obj)
{
    while (obj && obj != Object.prototype)
    {
        // Get all of the object's property names, even non-enumerable ones
        var keys = Object.getOwnPropertyNames(obj);
        keys.forEach(function(key)
        {
            // We should restrict this check to function objects; left
            // as an exercise for the reader...
            if (key !== "caller" &&
                key !== "callee" &&
                key !== "arguments"
                )
            {
                var value = obj[key];
                var type = typeof value;
                debugWriteLine('object = ' + name + ' | property = ' + key + ' | type = ' + type + ' | value = ' + value); 

                if (type === 'object')
                {
                    return showData(name + "." + key, value);
                }
            }
        });

        // Get the object's prototype
        obj = Object.getPrototypeOf(obj);
    }
}

showData('$', $);

.as-console-wrapper {
  max-height: 100% !important;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

这篇关于javascript通过对象递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆