V
V
virtualhero2017-07-24 20:56:50
JavaScript
virtualhero, 2017-07-24 20:56:50

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...
}

then all properties and methods of the object will be available, which in itself excludes the normal api.
If using modules:
var ModuleName = (function() {
    // Private
    return {
        // Public...
    };
})();

Here you can already create a normal api, but less convenient for me.
What would you advise me to choose?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Negwereth, 2017-07-24
@virtualhero

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.

N
Nazar Mokrinsky, 2017-07-24
@nazarpc

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 question

Ask a Question

731 491 924 answers to any question