JSFiddle - React, Tailwind, and code Playground

by Pankaj Kargirwar

JavaScript

class MainClass {
  constructor() {
    this.property = 'value';
    this.subclassInstance = new SubClass(this); // Pass the main class context
  }

  mainMethod() {
    console.log('Main method called');
  }
}

class SubClass {
  constructor(mainClass) {
    this.mainClass = mainClass; // Store a reference to the main class context
  }

  subclassMethod() {
    console.log(this.mainClass.property); // Access property of the main class
    this.mainClass.mainMethod(); // Call method of the main class
  }
}

const instance = new MainClass();
instance.subclassInstance.subclassMethod(); // This will access properties and methods of the main class