Not to be hyperbolic, but the continual promotion of the "module" pattern in JavaScript is some of the worst advice you can give.
JavaScript has prototypes for a reason -- use them. By using the "module" pattern to build objects, you create a separate copy of every function for every instance of every object you create. If you're just creating a handful of objects, it's no big deal, but if you're creating a large number of objects, it's horribly CPU and memory inefficient.
Modern JS runtimes like Chrome/V8 can create and store a million small objects with prototypes and "new" in a couple seconds, using just a dozen or so megabytes of RAM. Creating the same million small objects with the "module" pattern takes minutes, uses many hundreds of megabytes of memory, and often crashes the browser.
And that's just the pragmatics -- there are deeper semantic reasons to use real prototypes.
I think the author mis-understands memory management. The following line argues that, without a reference to the prototype, the object is smaller:
"The other way it differs from the constructor pattern is that the prototype class is not instantiated. Which reduces the memory consumption of the object (among other things)."
Admittedly, there is some ambiguity because the second statement is a fragment. The way Crockford is quoted seems to support this fallacy, though.
Echoing what jashkenas said: anything you may save on a reference to the function's prototype (minimal), you lose when creating copies of each member function.
What Crockford was speaking about in his post was using "new" directly on a function definition. In that specific case, the reference to the prototype is useless, since it is blank. In the OP's "Mammal" example, the prototype reference provides key functionality that is well worth the cost.
Yes I suppose that's true. I think it all depends on what your case is and preference, but yes memory is an issue with the module method. Just out of curiosity what are the best tools out there to test memory consumption of javascript snippets? It'd be interesting to see prototype/module/whatever_else memory usage side by side.
The browsers are getting better at optimizing memory use for small closures all the time, but there's still a long way to go. Here's a screenshot of the Chrome heap profiler for both cases:
I have one question. How to handle private methods in prototype/module pattern? If I declare private function in obj function declaration I can not access it in public functions attached to the prototype of obj. This is also true for module pattern. This problem arises when I need to add extra method later. How to overcome this situation?
This is all true. But people might be more convinced by the bigger reason: developer productivity.
The module pattern boils down to trying to bring the ideas of privacy from Java into JavaScript. Java has good tools (IDEs) for interacting with, debugging, and testing objects with private data and methods. JavaScript does not.
Once you build your encapsulated module, you find yourself adding extra getters and setters to provide visibility to the code you're writing or testing. Then it turns out to be very convenient to use some of those in the API you export to other modules, and you may or may not remember to remove them before deploying, so you end up losing any supposed benefit of encapsulation.
You are not being hyperbolic. The promise of strong encapsulation is very compelling, but this pattern is only appropriate for Modules (hence the name).
I suspect many JS developers head down this blind alley and get burned.
JavaScript has prototypes for a reason -- use them. By using the "module" pattern to build objects, you create a separate copy of every function for every instance of every object you create. If you're just creating a handful of objects, it's no big deal, but if you're creating a large number of objects, it's horribly CPU and memory inefficient.
Modern JS runtimes like Chrome/V8 can create and store a million small objects with prototypes and "new" in a couple seconds, using just a dozen or so megabytes of RAM. Creating the same million small objects with the "module" pattern takes minutes, uses many hundreds of megabytes of memory, and often crashes the browser.
And that's just the pragmatics -- there are deeper semantic reasons to use real prototypes.