Advertisement

07.18.2007 at 03:39AM PDT, ID: 22703743
[x]
Attachment Details

advanced JavaScript - retrieve the parent object reference.

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.2
Tags:

javascript, parent, object

Sorry this is quite long and complicated - only for the real experts - but quite fun I think.

I think it's easier to explain stuff with code - so here's a non-working example:

-------------------------------
function lib() {
    this.parent = <the Object instance>
}
Object.prototype.lib = new lib();

var objectInstance = new Object();
objectInstance.hello = "hello world!";

alert(objectInstance.lib.parent.hello);
-------------------------------

This doesn't work, obviously. But I hope it illustrates what I'm trying to do - access the parent object (Object) from within the child object (lib).

I came up with this solution, which overloads Object's constructor to initialise the lib object with the reference to the parent object stored:

---------------------------------------
var lib = function(parentObj) {
    this.parent = parentObj;
}

var oldConstructor = Object;
Object = function() {
    this.lib = new lib(this);
    oldConstructor();
}

var objectInstance = new Object();
objectInstance.hello = "hello world!";

alert(objectInstance.lib.parent.hello);
---------------------------------------

Success! alert displays "hello world!". BUT, the reason I wanted to do this was so that lib.parent would be available in all objects, and this isn't the case because the constructor doesn't seem to be inherited through prototype:

--------------------------------
var lib = function(parentObj) {
    this.parent = parentObj;
}

var oldConstructor = Object;
Object = function() {
    this.lib = new lib(this);
    oldConstructor();
}

var anObject = function() {}
anObject.prototype = new Object;

/*Object.prototype.lib = {parent: this}*/

var objectInstance = new anObject();
objectInstance.hello = "hello world!";

alert(objectInstance.lib.parent.hello);
--------------------------------

Alert now displays "undefined" again. And it would be the same if objectInstance contained a HTMLElement or an Array object. Object's constructor is being called, but as the constructor only adds "lib" to the Object itself and not the Object's 'prototype', it doesn't get inherited by anObject. I can't add it to the Object's prototype, because this doesn't get created until after Object has been initialised.

I can't work out a solution to this, apart from putting "this.lib = new lib(this)" in the constructor of every type of object, which is obviously infeasible.

Can anyone think of a solution?
Thanks, Robin.
Answered By: dbritt
Expert Since: 05/30/2003
Accepted Solutions: 368
dbritt has been an Expert for 5 years 7 months, during which he has posted 1987 comments and answered 368 questions. dbritt is just one of 1755 experts in the Scripting Languages Zone. 3 experts collaborated on this answer, which was graded an "A" by the asker.
 
 
 
 
20081119-EE-VQP-47