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
  }
}
3
  • 1
    your are saying that let table = new Table() and table.cells.edit() is not working? Commented Dec 16, 2017 at 13:42
  • 1
    I tested it, it ran properly. Commented Dec 16, 2017 at 13:48
  • No, don't do this. Commented Dec 16, 2017 at 14:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didnt know return keyword works like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.