Results 1 to 7 of 7

Thread: Parent of an object

  1. #1
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Parent of an object

    Sorry, this is really dumb and I ought to know. If I make an object "obj1", and give it another object "obj2".
    Code:
    var obj1 = new Object ();
    obj1.obj2 = new Object ();
    Can I do something like obj1.obj2.parent to get back to obj1? THis isn't a great example but it would help if someone could tell me if this is possible and how.

    Thanks,
    Stephen

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, you can't (but the above is better expressed as var obj1 = { obj2: {} }). When executing a function on one of those objects, the direct 'parent' at calltime is accessible as 'this'; see my article on context.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    Well, I was doing it in a prototype (is it called that?) so I wanted to get the parent of "this" like "parent.parent".
    I did trying this to find a workaround:
    Code:
    var obj1 = new Object ();
    obj1.setParent = function (prop) {
    this [prop].parent = this;
    }
    obj1.obj2 = new Object ();
    obj1.setParent ("obj2");
    obj1.text = "txt1";
    obj1.obj2.text = "txt2";
    alert (obj1.text);
    alert (obj1.obj2.text);
    alert (obj1.obj2.parent.text);
    obj1.text = "txt3";
    alert (obj1.obj2.parent.text);
    (sorry about the new Object (), I'm just accustomed to it and haven't used anything else)
    It says:
    Code:
    txt1
    txt2
    txt1
    txt3
    I tested it in IE and FF and it works. So then ".parent" works as a kind of link to the parent, but I don't want to have to run "setParent" for every single property for every single object. Is there a method in object that is run when it a property is added to the object or another way to apply the parent property to everything without doing it manually?
    Also, how does my code actually work? What makes ".parent" a link to the parent and not a copy of it? I've seen thing like this and that's how I came up with it but...

    Thanks,
    Stephen

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yeeeeah, that kind of works but it's rather ugly.

    In Javascript all objects are passed by reference. In fact, there's not even a built-in function to make a copy of an object, and it's not entirely possible to do it perfectly due to JS' weird type system.

    new Object() is just a waste of bytes, it offers no advantage over literal notation and doesn't allow you to assign properties at creation time. The literal notation also makes the structure of your objects clearer:
    Code:
    var obj1 = {
      setParent: function (prop) {
        this[prop].parent = this;
      },
      obj2: {
        text: "txt2"
      },
      text: "txt1"
    };
    obj1.setParent("obj2");
    obj1.obj2.text = "txt2";
    alert(obj1.text);
    alert(obj1.obj2.text);
    alert(obj1.obj2.parent.text);
    obj1.text = "txt3";
    alert(obj1.obj2.parent.text);
    Careful with your spaces. Convention in C-like syntax puts spaces after flow-control statement keywords, but not before function calls or property lookup.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    So is there an easy way to aply it to everything or should I make a big ugly loop cycling through every object and doing it? Lol, thad be terrible.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There is not a nicer way of doing it, no -- well, not in browsers at least. You can use getters/setters, but IE doesn't support them.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. The Following User Says Thank You to Twey For This Useful Post:

    ??? (08-20-2008)

  8. #7
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    Well thanks.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •