Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classes vs modules in Node.js

Tags:

node.js

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.

like image 988
JKSOI123a Avatar asked Sep 29 '17 17:09

JKSOI123a


People also ask

What is the difference between class and module?

What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).

What is classes in Nodejs?

Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics.

What is meant by modules in node JS?

In Node. js, Modules are the blocks of encapsulated code that communicates with an external application on the basis of their related functionality. Modules can be a single file or a collection of multiples files/folders.


1 Answers

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)

In the example above the private

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.

like image 121
basickarl Avatar answered Oct 11 '22 12:10

basickarl