js json对象从子集找父级怎么找,有方法吗

2025-05-13 18:29:14
推荐回答(1个)
回答(1):

JavaScript 本身是不支持这种查找的, 因为"子对象"只是父对象中的一个引用, 它也可以被其它对象引用, 这样一个"子对象"就可能有多个"父对象". 能实现的是在代码运行中获取它的上级对象.

var Obj = function()
{
    this.child = {
        parent: {},
        get_parent: function()
        {
            return this.parent;
        }
    };
    this.init = function()
    {
        this.child.parent = this;
    };
    this.init();
}

var o = new Obj;

console.log(o.child.get_parent());