PDA

View Full Version : Parent of an object



???
08-18-2008, 04:18 PM
Sorry, this is really dumb and I ought to know. If I make an object "obj1", and give it another object "obj2".


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

Twey
08-18-2008, 05:24 PM
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 (http://fn-js.info/context).

???
08-19-2008, 05:39 PM
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:


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:


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

Twey
08-19-2008, 06:21 PM
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:
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.

???
08-19-2008, 06:37 PM
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.

Twey
08-19-2008, 09:46 PM
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.

???
08-20-2008, 04:39 PM
Well thanks.