David Sevcik

JavaScript and Ruby expert, Micro-services architecture researcher and entrepreneur at start

Design Patterns in Node.js: Module Pattern

Today I start the series of blog spots dedicated to design patterns used in Node.js development. As developers you are probably familiar with the GoF design patterns. These work best with the programming languages using class inheritance but JavaScript belongs to the prototype-based category of languages. Moreover the Node.js is specific for an asynchronism. Because of this I see useful to revise already known patterns from GoF and add some more specific to JavaScript or Node.js.

There is no order of particular patterns in this series. I plan to simply pick those which I use the most in my current projects. For start I have chose a Module Pattern.

 Code first

 Synchronous version

var privateStaticVariable = 'sample';

// constructor function
exports.createInstance = function(context) {
    var privateVariable = 10;

    var privateFunction = function() {
        return privateVariable

Continue reading →