class - JavaScript ES6: Grouping methods in es6 classes? - Stack Overflow

A few of us are trying to create a JavaScript library to quickly run JSON queries on a RESTful API.What

A few of us are trying to create a JavaScript library to quickly run JSON queries on a RESTful API.

What I'd like to do is group a set of methods relative to their purpose.

For example;

Through the API I am able to get user attributes. Instead of having all of these methods under the main object, I'd like to group them within the API class object.

i.e. Transform this:

myAPI.getUserById()

To This:

myAPI.User.getByID()

myAPI.User.getByName()

We'll use the code below as a simple example. How might I have my User methods nested within a User Object within myAPI Class??

class myAPI {
  constructor(url) {
    this.url = url;

    //Code to connect to instance...

  }

  getUserById(userId){
    // function
  }
}

Solved

class myAPI {
  constructor(url) {
    this.url = url;
    this.UserAPI = new UserClass(this);

    //Code to connect to instance...

  }

  getUserById(userId){
    // function
  }
}

class UserClass {
  constructor(parent){
    this.myAPI = parent;
  }
}

A few of us are trying to create a JavaScript library to quickly run JSON queries on a RESTful API.

What I'd like to do is group a set of methods relative to their purpose.

For example;

Through the API I am able to get user attributes. Instead of having all of these methods under the main object, I'd like to group them within the API class object.

i.e. Transform this:

myAPI.getUserById()

To This:

myAPI.User.getByID()

myAPI.User.getByName()

We'll use the code below as a simple example. How might I have my User methods nested within a User Object within myAPI Class??

class myAPI {
  constructor(url) {
    this.url = url;

    //Code to connect to instance...

  }

  getUserById(userId){
    // function
  }
}

Solved

class myAPI {
  constructor(url) {
    this.url = url;
    this.UserAPI = new UserClass(this);

    //Code to connect to instance...

  }

  getUserById(userId){
    // function
  }
}

class UserClass {
  constructor(parent){
    this.myAPI = parent;
  }
}
Share Improve this question edited Apr 19, 2016 at 19:55 elias91 asked Mar 17, 2016 at 15:39 elias91elias91 836 bronze badges 1
  • Possible duplicate of Organize prototype javascript while preserving object reference and inheritance - no, there's no good way to do this using only one class. – Bergi Commented Mar 17, 2016 at 16:43
Add a ment  | 

1 Answer 1

Reset to default 11

You could use position:

class UserAPI {

    constructor(url) {
        this.url = url;
    }    

    getById() {
    }
}

class API {

    constructor(url) {

        this.url = url;
        this.User = new UserAPI(url);
    }
}

var myAPI = new API();

myAPI.User.getById();

This way, you can separate all kind of groups into classes. You can even discriminate different implementations of API's based on the user's requirements.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744053058a4550425.html

相关推荐

  • class - JavaScript ES6: Grouping methods in es6 classes? - Stack Overflow

    A few of us are trying to create a JavaScript library to quickly run JSON queries on a RESTful API.What

    9天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信