Skip to content

Instantly share code, notes, and snippets.

@stevepentler
Created March 30, 2016 05:22
Show Gist options
  • Save stevepentler/8c9f4d808d0abdc75d1996072c2d1d0c to your computer and use it in GitHub Desktop.
Save stevepentler/8c9f4d808d0abdc75d1996072c2d1d0c to your computer and use it in GitHub Desktop.

#####In the context of Node, what is a module?

  • modules are self-contained chunks of code that serve a specific purpose
  • equivalent to a Ruby class
  • module.exports is anything that is exposed from one file to another. It can be a constructor, function, variable, etc.

#####The code examples from the second blog post look very different from the first. Why?

  • the second article uses RequireJS, which loads asynchronously and packages the module in a define/require function.
  • in the example, they return variables/methods within the define function. This define method seems to wrap everything, but only the pieces that are exported are returned.
  • can pass other module dependencies into the define function ex: define(["models/Person", "my/utils"]), function (Person, utils) {...}
  • require is used when the dependencies are loaded immediately
  • Should we default to require or define?

  • module.exports to expose anything we want
    • module.exports.User; => var u = new user.User();
    • module.exports = User; => var u = new user();
    • function
      • var power = function(level) { return x}
      • module.exports = powerLevel => require('./powerLevel')(9050)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment