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.
Bookmarks