Is it possible to create a method in a class that references another class in javascript?

Apologies if this is a newbie question but I’m learning how to use classes in JavaScript and am running into something I’m not sure is possible. Maybe my syntax is incorrect or there’s a better way to go about this.

I have 2 files, each contain a class to construct an object which I’m calling in a third file (my questions are below this sample JS):

file1

import { Second } from "./file2";
        
class First {
 constructor(
   parameter1,
   parameter2
 ) {
   this.parameter1 = parameter1;
   this.parameter2 = parameter2;
   }
  method1() {
   this.parameter1 = this.parameter1 + " " + Second.parameterA
   return this.parameter1;
   }
 }

 export { First };

file2

class Second {
  constructor(
    parameterA,
    parameterB
  ) {
    this.parameterA = parameterA;
    this.parameterB = parameterB;
  }
}

export { Second };

file3

import { First } from "./file1";
import { Second } from "./file2";
    
const first = new First(
  "someStringForParameter1",
  "someStringForParameter2"
);
    
const second = new Second(
  "someStringForParameterA",
  "someStringForParameterB"
);

console.log(first.method1());

The issue is when I call method1() in the third file I get parameter1 but parameterA is undefined in the console. I’m not sure why this is the case. Is this a legal use of imported classes within another class?

I’ve tried importing the files in different orders (not surprised that didn’t work) and moving the the Second class into the same file as the First class. I’m able to console.log() all parameters of the First and Second class from the third file successfully but the method I’m calling only returns "someStringForParameter1 undefined". My goal is for console to state "someStringForParameter1 someStringForParameterA". There are no errors in console and the linter (ESLint) I’m using in VS Code isn’t highlighting anything. Any help on this would be much appreciated!

>Solution :

The issue doesn’t have anything to do with modules. It’s Second.parameterA – that will reference a property directly on the Second class, but no such property exists.

To see it when you reference Second.parameterA, you’d have to do

class Second {
  static parameterA = 'something'

or, after the class definition

Second.parameterA = 'something';

But if you want the argument passed into Second to be shown, you’ll have to get First to have a reference to that Second instance somehow – perhaps with a parameter to method1, like:

  method1(secondInstance) {
    this.parameter1 = this.parameter1 + " " + secondInstance.parameterA
    return this.parameter1;
  }
console.log(first.method1(second));
class First {
  constructor(
    parameter1,
    parameter2
  ) {
    this.parameter1 = parameter1;
    this.parameter2 = parameter2;
  }
  method1(secondInstance) {
    this.parameter1 = this.parameter1 + " " + secondInstance.parameterA
    return this.parameter1;
  }
}
class Second {
  constructor(
    parameterA,
    parameterB
  ) {
    this.parameterA = parameterA;
    this.parameterB = parameterB;
  }
}

const first = new First(
  "someStringForParameter1",
  "someStringForParameter2"
);

const second = new Second(
  "someStringForParameterA",
  "someStringForParameterB"
);

console.log(first.method1(second));

Or pass in the instance when calling First‘s constructor, or somehing like that.

Leave a Reply