有没有其他方法可以在javascript中引用grandParent对象

blu*_*dtt 3 javascript

我有以下代码:

var objectParent:{
     child1:{
       test1:function(){},
       test2:function(){}
      },

      child2:{
       demo1:function(){},
       demo2:function(){},
       parent:this  // gives child2
       grandparent: .... // need to reference objectParent here

      }

    }
Run Code Online (Sandbox Code Playgroud)

我们可以使用这个关键字来引用对象,但是对于大对象还是我的意思是父对象的父对象呢?

现在,我使用祖父母:objectParent来引用父对象

有没有其他方式像这个选择器来引用 parentObject?

这些是我的编码是坏的,好的还是更好的?

Bar*_*mar 6

如果您的所有对象都有一个parent指向其父对象的属性,则可以使用object.parent.parent.