Prany
Prany

Reputation: 2133

Accessing objects from one class to another in ES6

First of all apology for asking this as I am new to ES6 javascript. My class structure is something like -

Filename - Ab.js

class A{
method A(){
const a = '1'
//could have more const in this method

}}

class B{
method B(){
const b='2' 
//could have more const in this method
}}

Now I want to access this class in another files say C.js

class c{
 method c()
{
      //here I want to access A and B like
    const c= A.A.a // this should return 1
 }}

However I tried by exporting the default class in Ab.js and importing the same in C.js, I was able to access the value of the object in C.js but if you have multiple classes in Ab.js, it is not allowed. May I know why ? .. any solutions will be appreciated.

Upvotes: 0

Views: 204

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44105

Use static methods and return an object:

class A {
  static A(a = 1, b = 2) {
    return { a, b };
  }
}

Usage:

A.A().a; // 1
A.A().b; // 2

Upvotes: 1

Related Questions