Refactoring with prototypical inheritance
The functional mock-up created in the previous recipe, Writing a functional module mock-up, can be useful for getting familiar with a concept and may be perfectly adequate for small, simple modules with narrow scope.
However, the prototype pattern (among others), often used in Node's core modules, is commonly used by module creators and is fundamental to native JavaScript methods and objects.
Prototypical inheritance is more memory efficient. Methods sitting on a prototype are not instantiated until called; instead of being recreated on each invocation, they're reused.
On the other hand, it can be slightly slower than our previous recipe's procedural style because the JavaScript engine has the added overhead of traversing prototype chains. Nevertheless, it's (arguably) more appropriate to think of and implement modules as entities in their own right, which a user can create instances of (for example, a prototype-oriented approach). This makes the modules...