This question applies especially to Node.js, but also to JavaScript in general.

I started working on a simple web app in Node.js. I'm relatively new to Node and JavaScript, and come mainly from C# and Java.

In my app, obviously I need objects. In C#, I would create a class and instantiate it to get an object.

In Node.js however, I have two options:

  1. Creating a 'class' in a Node module of it's own, and later requireing and instantiating it (like I would do in C#).
  2. Creating an object in a Node module of it's own, and using it later by requireing it directly.

I'm trying to decide what the better approach in general is.

I'm leaning strongly towards approach 1, because it lets me instantiate the desired class anytime I want, instead of using a global singleton.

On the other hand, maybe it feels like the better approach simply because I'm used to it, coming from C#. Possibly I'm just trying to do C# in JS?

So my question would be: in Node and generally in JavaScript, when in need of an object to implement a piece of functionality, should I go with classes or with plain objects?

  • 1
    I wouldn't design yourself into a singleton design unless the type of object could NEVER have more than one. If you might ever want more than one, then you may as well design an object that you can instantiate more than one of.
    – jfriend00
    Feb 8 '15 at 23:48

My insight in this matter might not qualify for a full answer, but I think you are comparing to valid approaches, who's differences aren't a matter of style, but of solving different problems.

If you fear of trying to do C# in Javascript, maybe you should start by questioning a more essential premise of yours:

In my app, obviously I need objects.

More often than not, in Javascript this statement is not as unquestionable as you might think. Probably, if you are considering a singleton as an option, you are just looking for a namespace where you can expose a set of functions. In this case, you could just add these functions to module.exports, which is already an object that acts as the namespace of the module.

This is an interesting read about the many patterns that can be used when designing the interface of a module in Node.js.

  • I see. So do you agree that often I can settle with plain modules, but when I do need an object I should create a 'class'?
    – Aviv Cohn
    Feb 9 '15 at 13:45
  • Precisely. It's understandable the urge to think about classes when coming from C# and Java. But Javascript (like many other languages) offer a wider range of patterns that fulfil some of the needs that would be solved with classes in C# or Java. These languages are called multi-paradigm, because they are designed with multiple programming paradigms in mind, not just OOP. This doesn't mean you shouldn't use classes. If OOP is the paradigm of your choice, you will define and use objects. But not if it is just to solve a need that is already natively solved, like modules. Feb 9 '15 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.