Answer the question
In order to leave comments, you need to log in
JavaScript Application Development: ES6 Classes or Modules?
What is preferable to use to create api when developing an application, ES6 classes or modules?
For example, if you use an ES6 class:
class ClassName {
constructor() {}
// Methods...
}
var ModuleName = (function() {
// Private
return {
// Public...
};
})();
Answer the question
In order to leave comments, you need to log in
In ES6, the file itself is a module. This is what default and the usual export are for. Classes are better used to describe entities when inheritance is needed, etc.
That is, if you use instances of entities created on the basis of data from the API in your code, these entities know about other entities, have hierarchical relationships, and must be able to receive related data - then the choice is up to classes. And that, there still it is necessary to break into normal methods and on static.
And if you just get some lists, then the usual ES6 modules are enough for the head. But I emphasize - ES6 modules, and not that ancient implementation of the revealing module from your example.
These are not contradictory things, and what you wrote is not an ES2015 module.
It is very possible to use classes together with modules.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question