Home > Net >  Instantiate an object that stores a reference of another object (inaccessible)
Instantiate an object that stores a reference of another object (inaccessible)

Time:11-02

I'm a C# developer and my girlfriend started studying JS. Although I don't like this language, I try to help him with the objects.

I want to access my 'Season' object from the 'Serie' object which stores a reference to it, but impossible and I don't understand.

I'm not used to being assisted in a programming language, so much by dynamic typing (although C# can do it)

class Serie
 {
   constructor(title, season, year)
   {
     this.title = title;
     this.season = season;
     this.year = year;
   }
 }

class Season
{
  contructor(number, episode)
  {
    this.number = number;
    this.episode = episode;
  }
}

class Episode
{
  constructor(title, duration, hasBeenWatched)
  {
    this.title = title;
    this.duration = duration;
    this.hasBeenWatched = hasBeenWatched;
  }
}

let episode1 = new Episode("I'am s**", 45, false);
let saison1 = new Season(1, episode1);
let serie1 = new Serie("The Story of Tau", saison1, 1999);

document.querySelector('#first-episode-info').innerText = `Série: ${serie1.title}
Saison: ${serie1.season.number} 
  1 ${serie1.season.episode.title}
          ${serie1.season.episode.duration} min
          ${serie1.season.episode.hasBeenWatched ? 'Already watched' : 'Not yet watched'}
${serie1.year}`;

thank you

I want to access the properties of my 'Season' object from another object

I did a lot of research on the internet but I can't find the solution (the evolution of the language changes the syntax)

Please understand that I have no experience in this language.

CodePudding user response:

You made a typo.

class Season
{
  // contructor -> con(s)tructor
  constructor(number, episode)
  {
    this.number = number;
    this.episode = episode;
  }
}
  • Related