-1

I have been doing some exercises on the interesting "level up your coding" site, Exercism.io and they use the CommonJS style modules for their code samples and testing with Jasmine. I've always thought modules were a hassle I didn't want to deal with, but in these bite size chunks, they look like they could be very useful to begin using in my Single Page Applications. So I've been Googling around and searching Github for some good samples for using CommonJS Modules - and still haven't found one that explains in detail what the main patterns are, and how they differ. For example, one answer I submitted looked like this:

var HelloWorld = function () {};

HelloWorld.prototype.hello = function () {
  return 'Hello, World!'
};

module.exports = HelloWorld;

But another one looked like this

var Bob = function () {
    this.hey = function (input) {
        input = input.split('');
        if (input.indexOf('!') >= 0) {return 'Whoa, chill out!'}
        if (input.indexOf('?') >= 0) {return 'Sure.'}           
        return 'Whatever.'
    };

}

module.exports = Bob;

Specifically I'm wondering what the difference is between nesting a function inside the parent definition, as done with the Bob hey() function, as opposed to the way the HelloWorld hello() uses the prototype instead.

Stacey Reiman
  • 666
  • 4
  • 13

1 Answers1

2

To start with the two functions you gave as examples are completely different from each other and have different purposes.

Based off of the code you have as an example, the way you are calling them is also incorrect.

For your Bob example, all you are doing is assigning a function to a variable. In order to call it, you simply have to do Bob(). If you do a Bob.hello() you are going to get an error.

The HelloWorld on the other is not just a function, well.. It is, since you declared it as an empty function that is what it will call if you do HelloWorld(). However you defined hello as it's prototype function, for you to invoke that directly you would have to do HelloWorld.prototype.hello(). I reckon that these are used mainly to alter behaviours of existing objects or functions.

You are asking what are the most efficient ways of writing modules, but in reality there is not right answer for that. All a module is is a piece of code that can be exported and reused by other files. They can be functions, objects, simple variables whatever you'd like!

So basically you can do all these:

// moduleThatExportsANumber.js
module.exports = 1

// moduleThatExportsAnObject.js
module.exports = {}

// moduleThatExportsAFunction.js
module.exports = function () { return 'say somethign!'}

// main.js Lets call all the modules!
const number = require('./moduleThatExportsANumber)
const object = require('./moduleThatExportsAnObject)
const function = require('./moduleThatExportsAFunction)

console.log(number) // 1
console.log(object) // {}
console.log(function) // function () { return 'say somethign!'}
console.log(function()) //say somethign!

The whole thing about modules is simply writing stuff in a file, exporting that stuff, which in the case of CommonJS is done through module.exports = [whatever you are exporting], and later importing, which for CommonJS is require('./filename')

Now... Going back to the original thing that was asked, your cheatsheet. I don't know any CommonJS ones unfortunately, however here is a decent blog post about the CommonJS module system and here is a JavaScript one that you may like as well.

theJuls
  • 6,788
  • 14
  • 73
  • 160
  • sorry the Bob.hello() was a typo, I fixed that! I am new to writing modules, so I just used the formats found in the demo files for Exercism.io - the hello() function is used in the Jasmine spec by first creating an instance - var helloWorld = new HelloWorld(); - then the actual fn - helloWorld.hello() – Stacey Reiman Dec 21 '17 at 02:39