Skip to content

Instantly share code, notes, and snippets.

@makeusabrew
Last active December 25, 2015 20:59
Show Gist options
  • Save makeusabrew/7038858 to your computer and use it in GitHub Desktop.
Save makeusabrew/7038858 to your computer and use it in GitHub Desktop.
Given a tree structure which indicates some level of inheritance / dependency with
some concrete objects at the end of the tree e.g.
src/
adapter/
mocha/
coffeescript.js
javascript.js
Assume that at each level of the tree (excluding src) there is a relevant 'base'
object which those below it rely on (e.g. javascript.js relies on a mocha object
which relies on an adapter object).
Using Node's require system, those base objects could be fulfilled either like so:
src/
adapter.js
adapter/
mocha.js
mocha/
coffeescript.js
javascript.js
OR
src/
adapter/
index.js
mocha/
index.js
coffeescript.js
javascript.js
Since using Node's require(), calling:
require("./src/adapter");
Will find both src/adapter.js and src/adapter/index.js
From the descendent object's point of view it's equivalent too:
// src/adapter/mocha/coffeescript.js
require("../mocha") // finds ../mocha.js or ../mocha/index.js
The only difference is that using the index.js approach, child objects can
also require like so:
require("./") // finds ./index.js
Or:
require("../mocha/index")
Using the index approach is nice in that all relevant objects are 'packaged'
together in one single folder, but it could potentially obscure on first
glance which is the 'base' object (index.js) and which are descendents. The
alternative approach feels a bit more traditional and when quickly tabbing
through the tree makes it clear that there is a base / descendent relationship.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment