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

  • SEARCH
  • Home
  • 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 3691404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:08:39+00:00 2026-05-19T04:08:39+00:00

Using JavaScript how do I create a subclass that inherits values from the parent

  • 0

Using JavaScript how do I create a subclass that inherits values from the parent class? These values I need to inherit are defined by the parameters of the parent class. I want to have a one-to-many relationship between the parent class and the child class. Here is my example code where a Song consists of one or more Tracks:

function Song(x){
  this.x = x;
}

function Track(y){
  Song.call(this);
  this.y = y;
}

Track.prototype = new Song;


var mySong = new Song(1);
mySong.guitar = new Track(2);
mySong.bass = new Track(3);
// goal is to output "1 1 2 3" but currently outputs "undefined undefined 2 3"
console.log(mySong.guitar.x + " " + mySong.bass.x + " " + mySong.guitar.y + " " + mySong.bass.y );  

This question is similar to this sub-class question (http://stackoverflow.com/questions/1204359/javascript-object-sub-class) but uses variables defined by parameters. Right now when I try to call parentObject.childObject.parentVariable it returns undefined. What do I have to change in the code to make this work or is there a better way to code this one-to-many parent/child relationship?

  • 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-19T04:08:39+00:00Added an answer on May 19, 2026 at 4:08 am

    So, it looks like you don’t want inheritance, you want composition.

    I’ll try to find a good link.

    I didn’t find any good links and as I looked at your code more, I got more confused. Here is something that accomplishes what you want, but I’m not sure it’s really useful especially since it does a few things I would not normally consider.

    function Song(x){
      this.x = x;
    }
    
    Song.prototype.addTrack = function(name, track) {
      this[name] = track;
      track.x = this.x;
    }
    
    function Track(y){
      this.y = y;
    }
    
    
    var mySong = new Song(1);
    mySong.addTrack('guitar', new Track(2));
    mySong.addTrack('bass', new Track(3));
    
    console.log(mySong.guitar.x + " " + mySong.bass.x + " " + mySong.guitar.y + " " + mySong.bass.y );
    

    Seems to me that a better solution would be:

    function Song(x){
      this.x = x;
      this.tracks = [];
    }
    
    Song.prototype.addTrack = function(track) {
      this.tracks.push(track);
    }
    
    Song.prototype.output = function() {
       // loop on this.tracks and output what you need using info from 'this' and 
       // each track
    }
    
    
    function Track(y){
      this.y = y;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.