最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there other way to reference grandParent object in javascript - Stack Overflow

programmeradmin2浏览0评论

I have following code:

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

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

      }

    }

We can reference object by using this keyword, But what for grand object or i mean parent of parent object?

Now, i am using grandparent:objectParent to reference parent object

Is there other way like this selector to reference parentObject?

Are these my coding is bad,good or better?

I have following code:

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

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

      }

    }

We can reference object by using this keyword, But what for grand object or i mean parent of parent object?

Now, i am using grandparent:objectParent to reference parent object

Is there other way like this selector to reference parentObject?

Are these my coding is bad,good or better?

Share Improve this question edited Feb 23, 2013 at 3:45 blue ghhgdtt asked Feb 23, 2013 at 3:26 blue ghhgdttblue ghhgdtt 9214 gold badges11 silver badges16 bronze badges 5
  • 1 I don't think that var objectParent:{ ... } is even valid syntax – lmortenson Commented Feb 23, 2013 at 3:35
  • Then what is valid? Should i remove var? or add something else? – blue ghhgdtt Commented Feb 23, 2013 at 3:40
  • It should be var objectParent = { ... }. – Barmar Commented Feb 23, 2013 at 3:49
  • this won't give child2, it returns whatever the value of this is in the function you're running. You seem to be confusing object literals with object-oriented prototypes. – Barmar Commented Feb 23, 2013 at 3:50
  • Just because you nest objects does not mean you're creating some form of object graph (with raw JS). When you assign a child object you're assigning a reference to a different memory location, there is not actually a hierarchy created: just what are effectively flat references. If you want to point to a "parent" object, then you should provide a reference to that object within the context of what you're viewing as a "child" object. – Matt Whipple Commented Feb 23, 2013 at 3:51
Add a comment  | 

4 Answers 4

Reset to default 10

If all your objects have a parent property that refers to their parent, then can get the grandparent with object.parent.parent.

It's not real clear what you're trying to do.

If you have an element reference elem, then you can get its parent with elem.parentNode. You can get a parent's parent (e.g. a grandparent) with elem.parentNode.parentNode and so on.

If this isn't what you're trying to do, please explain in more detail what you mean by a grandparent object.


If you're not talking about DOM references at all and instead are asking about nested objects in plain javascript, then javascript does not contain any way to get the parent object that you are contained within. You would have to create a property on the child and set it if you need it that way after you've constructed the object (you can't set it with a static declaration either).

I had to do div.parentElement.parentElement to achieve it.

One way to see what to access is by logging div.__proto__ to the console which shows you a long list of properties & methods you can call.

There's no need to go the long way doing div.parentNode.parentNode.parentNode. Instead you can just do div.offsetParent and you'll get the grandparent of the div.

发布评论

评论列表(0)

  1. 暂无评论