JavaScript and Maps (in that order)

JavaScript style mistakes I see a lot

  • Edit: removed recursive reference, it was incorrect (my bad) but you should probably avoid recursive references in general.
  • Edit2: discuss on reddit
  • Edit3: forgot hoisting was such a hot button issue, switching to the hopefully less controversial statement that declarations should be your default

A quick run down of a few very common JavaScript style issues, that while aren’t outright errors you should probably avoid.

Function expressions, not on objects

This is when you write

var name = function () {};

instead of

function name(){}

the bottom version should be your default which you should use unless you have a reason to do it the other way. Some of the benefits include

  • hoisting (though you might passionately disagree)
  • reference to the functions name inside the function can’t be changed.
  • because you have to pick one for consistency and I’ve never seen project choose the top version.

using module.exports, to export an object

That is when you write

module.exports = {foo: function(){}, bar: function(){}}

instead of

exports.foo = function (){};

exports.bar = function (){};

the difference is that the bottom syntax can handle circular dependencies, and the top can’t, they are otherwise identical.

making me guess your exports key.

that is if you only export one thing from your module, doing

exports.someRandomName = thing;

as opposed to

module.exports = thing

unless you have circular dependency you have to deal with, making me write

var yourThing = require('yourModule').someRandomName;

is going to cause me to judge you.