javascript通过对象递归

时间:2017-10-17 06:36:44

标签: javascript object recursion

我想以递归方式遍历对象以进行调试和记录。 但是我的实际代码片段在第一个对象之后中断。

  import tkinter as tk
  from tkinter import *
  root = tk.Tk()


root.geometry("520x400")

frame1 = tk.LabelFrame(root, text="my_first_window", width=400,  height=200, bd=5)
Label(root, text="URL:").grid(row=0, column=1 ,sticky=W)

e1 = Entry(root)
e1.grid(row=0, column=2)
frame1.grid(row=0, column=0, columnspan=7, padx=8)

Button(root, text='BACK', command=lambda:print('BACK')).grid(row=8, column=1, sticky=W, pady=4)
Button(root, text='NEXT', command=lambda:print('NEXT')).grid(row=8, column=2, sticky=W, pady=4)
Button(root, text='CANCLE', command=lambda:print('CANCLE')).grid(row=8, column=3, sticky=W, pady=4)


mainloop()

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

谢谢 弗洛里安

1 个答案:

答案 0 :(得分:1)

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

您没有看到所有属性的原因是for-in循环仅循环遍历可枚举属性,并且许多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>
&#13;
&#13;
&#13;