Advertisement

07.18.2007 at 03:39AM PDT, ID: 22703743
[x]
Attachment Details
[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

advanced JavaScript - retrieve the parent object reference.

Asked by ratWonder in Scripting Languages, JavaScript, Dynamic HTML (DHTML)

Tags: , ,

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.Start Free Trial
[+][-]07.18.2007 at 04:33AM PDT, ID: 19512453

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2007 at 05:22AM PDT, ID: 19512707

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2007 at 09:24AM PDT, ID: 19515061

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2007 at 11:02AM PDT, ID: 19515840

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2007 at 11:04AM PDT, ID: 19515862

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2007 at 11:13AM PDT, ID: 19515963

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2007 at 03:22PM PDT, ID: 19517805

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2007 at 03:29PM PDT, ID: 19517846

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2007 at 03:59PM PDT, ID: 19517968

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2007 at 04:01PM PDT, ID: 19517976

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.20.2007 at 07:19AM PDT, ID: 19531198

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.20.2007 at 07:28AM PDT, ID: 19531301

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.20.2007 at 09:07AM PDT, ID: 19532463

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.20.2007 at 02:33PM PDT, ID: 19535454

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Scripting Languages, JavaScript, Dynamic HTML (DHTML)
Tags: javascript, parent, object
Sign Up Now!
Solution Provided By: dbritt
Participating Experts: 3
Solution Grade: A
 
 
[+][-]07.23.2007 at 02:25AM PDT, ID: 19545886

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.23.2007 at 02:27AM PDT, ID: 19545890

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.23.2007 at 07:08AM PDT, ID: 19547509

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32