【问题标题】:javascript recursive through an objectjavascript通过对象递归
【发布时间】:2017-10-17 06:36:43
【问题描述】:

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

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 的内容,但我需要 $ 的所有属性。

谢谢 弗洛里安

【问题讨论】:

    标签: javascript object recursion


    【解决方案1】:

    首先:你既不需要也不想eval 来做这一切。从$(对象)开始,然后在查看其属性之一时使用obj[key]。当该属性是非函数对象时递归。

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

    所以:

    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>

    【讨论】:

      猜你喜欢
      • 2017-07-18
      • 2021-05-09
      • 2019-01-19
      • 2019-01-22
      • 2020-04-24
      • 2020-09-26
      • 1970-01-01
      • 2015-11-18
      • 2011-12-12
      相关资源
      最近更新 更多