0

Good day,

I dont know if am can explain this well for you to help but i will like to use a an ES6 class to create an object that can be called like this. var = varaibles obj = objects

obj.var
obj.var.method
obj.var.var.method
obj.method.var

and so on.

I can only do one step

obj.var && obj.method

i will kind appreciate if one can help me here thanks

this is what i have done

class Table extends someClass {
  constructor() {
    super();
    this.column = {
      sort: () => {
        console.log("firing");
      },
      resize: () => {
        console.log("firing");
      }
    };

    this.cells = {
      edit: () => {
        console.log("firing");
      }
    };
  }

  myMethods() {
    //BLAH
  }
}
Ehigie Paul
  • 122
  • 1
  • 7

1 Answers1

1

From what I understood, here is my solution.

If I return a object full of methods, I can use that object as I like.

class someClass {

  // this is a parent method
  Parent() {
    console.log(`From a Parent`)
  }

  // a getter that returns an object
  get parentWithChild() {
    return {
      child() {
        console.log(`From a Child`)
      }
    }
  }

  // a function that returns an object
  Methods() {
    return {
      child() {
        console.log(`From a Child`)
      }
    }
  }
}

const cool = new someClass();
cool.Parent(); // From a Parent
cool.parentWithChild.child(); // From a Child
cool.Methods().child(); // From a Child

You can use similar pattern on the extended class too.

Md. Abu Taher
  • 17,395
  • 5
  • 49
  • 73