13

My question is, if it is better the use classes in Node.js or the function, or prototypes and modules, I know that most use the modules and function prototypes, but which is better?

And we most consider that classes have always been in all programming languages the best option to work.

1
  • 1
    Be very careful when asking questions like this because asking for "best" of anything is probably off-topic here on stackoverflow because that inevitably ends up being one persons opinion vs. anothers. Questions which primarily call for opinion are considered off-topic here. Commented Sep 29, 2017 at 17:40

2 Answers 2

28

classes vs. modules is the wrong question

The question you are looking for is classes vs. prototypes.

Both classes and prototypes are used by modules.

Modules (ES2015 Modules) are what JavaScript uses to be able to export and import code. Before JavaScript officially implemented modules there were hacks to do this and create code encapsulation which fixed issues such as pollution of the global scope; Immediately Invoked Function Expressions is an example. Node.js with CommonJS and Angular with AMD and several others tried to figure out how to do this in a better way and implemented their own 'module' system. I would like to think ES2015 Modules were influenced by other languages and CommonJS/Amd etc. and as a result, we now have a module system in JavaScript.

As for classes, they themselves create code encapsulation, but they cannot export themselves, that's what modules do. The same applies for the prototype based code, it creates code encapsulation but cannot export it self just like classes.

Now to put this together.

Example of a module, this example has nothing to do with classes or prototypes, only-a-module.js:

const privateHello = 'hello' // <- module code (private)
export const publicHello = 'hello'; // <- module code (public)

Example of a module that uses a class, module-that-exports-a-class.js:

export class Classes { // <- module AND class code (public)
    private privateHello = 'hello'; // <- class code (private)
    public publicHello = 'hello'; // <- class code (public)
} // <- class code (public)
const anotherPrivateHello = 'hello'; // <- module code (private)
export const anotherPublicHello = 'hello'; // <- module code (public)

Example of a module that uses prototypes, module-that-exports-a-prototype.js:

export function Prototypes() { // <- module AND prototype code (public)
    const privateHello = 'hello'; // <- prototype code (private)
    this.publicHello = 'hello'; // <- prototype code (public)
} // <- prototype code (public)
const anotherPrivateHello = 'hello'; // <- module code (private)
export const anotherPublicHello = 'hello'; // <- module code (public)

What makes this confusing to beginners coming from OOP languages is that for example in Java a class file (file.class) is automatically exported as a class, this is by default as this is how Java works. This is not the case in JavaScript. You must state what you are exporting.

Classes are an attempt to implement so called classical inheritance while prototype is, you guessed it, prototypal inheritance. These are two ways to deal with code-reuse and objects that keep their own state.

If we want to make things even more complicated, a good followup question is OOP vs. functional programming. But you can read that up in other places.

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

2 Comments

IMHO this is a more descriptive answer. This should be the accepted answer @JKSOI123a
Correct me if I'm wrong, but doesn't your prototype code snippet only tangentially relate to Prototypes? (It's rather just a constructor function) Prototype inheritance looks something more like this
12

My question is if it is better the use classes in nodejs or the function or prototypes and modules, i know that most use the modules and function prototypes, but which is better?

Your question seems a bit misguided. In node.js, one does not choose between classes, prototypes and modules.

First off classes in Javascript uses the prototype. The class syntax is just syntactical sugar for defining a constructor and methods on the prototype. When you use the class syntax, you are creating a constructor with a prototype and causing methods to be added to the prototype (under the covers). So, there is no tradeoff between using classes and using the prototype.

The only tradeoff there is there is between using the new class syntax to define your prototype vs. doing it the older, manual way.

Then, classes are pretty much orthogonal to modules. You use modules when you want to encapsulate a body of code in a module interface so you can reap all the benefits of using modules (easier sharing, reuse, documenting, testing, etc...). That module interface can be whatever you think is most appropriate for your circumstance. You can export a series of plain functions. You can export one or more object constructors. You can export factory functions that create objects for you that you can then use methods on. The module scheme is massively flexible so you can export pretty much any type of interface you want (and classes can be a part of that if you choose).

There is no "best" way to export things from modules because it depends upon your needs, your chosen architectural and coding style, etc...

3 Comments

let's say I work with classes, there is some way to export them as well as the modules?
and if there is any way, I would like to know if there is a difference in privileges
@Andreszl - Yes, you can just export a class. A class in Javascript it just a constructor function so you just export the constructor function as you would any function. I don't know what you mean about privileges. On stack overflow, you should generally not keep adding more questions after someone has answered your original question. If you have a complete question about privileges, then please post a new question that describes in detail what you're asking.

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.