Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6014493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:42:01+00:00 2026-05-23T02:42:01+00:00

I have some JS you’ll see below. I want the inner class object to

  • 0

I have some JS you’ll see below. I want the inner class object to be able to access it’s parent. It needs to access parent methods and properties. The way I’ve done it is working, but I’d like to know if there’s something I can do in the inner class constructor to get the parent, rather than the parent having to explicitly tell the child who it’s parent is. It seems clunky.

<html>
<body>
<script>
function ChildClass(name){
    //this.myParent=  no way of knowing .....
    this.myName=name;
    this.whereDidIComeFrom=function(){
        document.write(this.myName+ " came from " +this.myParent.dad+"<br>");
    }
}

function ParentClass(name){
    this.dad=name;
    this.myChild=new ChildClass(name+"son");
    this.myChild.myParent=this;   //only way I know for myChild to access parent
    this.myChild.whereDidIComeFrom();
}

var parentDavid = new ParentClass("David");
var parentJohn = new ParentClass("John");
</script>
</body>
</html>

The output from running that looks like this:

Davidson came from David

Johnson came from John

I ask because the above structure already exists in the project I’m maintaining. I can’t go redesigning the whole thing. It’s only now that the child object has to access it’s parent. Previously it hasn’t needed to. It would be nice to not have to change the parent class at all and do all my changes within the child. But if what I’ve got is basically “what you need to do” then so be it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:42:02+00:00Added an answer on May 23, 2026 at 2:42 am

    This is already the correct way to do this, in principle. You could make it somewhat fancier, providing an introduceParent() function in the child which checks, if the parent reference is already set, but that doesn’t change the core of the matter.

    While a child in your data model can belong to only one parent, Javascript doesn’t know that. There could be several parents referencing the same child, for example (hearsay is that sometimes happens in nature). Indeed, the child is not an “inner class” of the parent. The child and parent are merely associated, i.e. “they somehow know each other”, which is an unspecified relation, but neither is part (or property) of the other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some automatic de-serialization code that will set an object's properties using KVC
I have some ASP.NET web services which all share a common helper class they
I have some classes layed out like this class A { public virtual void
I have some kind of test data and want to create a unit test
Have some data in a sybase image type column that I want to use
I have some trivial markup that looks like the following: <li class=someclass> <=% t'model.attr'
I have some XML and need to be able to read the data within.
Have some text that needs to be replaced, searched around this website for all
I have some code that should remove the parent row of the clicked td
I have some couchDB database. and i want, as in mongodb , find one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.