Which is better sometimes can be determined by the context of their use.
Three limitations of how I choose Prototype and Closing coding methods (I actively use both):
- Productivity / Resources
- Compression Requirements
- Project management
1. Productivity / Resources
For a single instance of an object, any method is fine. Any speed benefits are likely to be negligible.
If I create 100,000 of them, for example, by creating a library of books, the Prototype method is preferable. The whole .prototype. functions will be created only once, instead of these functions being created 100,000 times using the Closure Method . The resources are not endless.
2. Compression
Use the Closure Method if compression efficiency is important (for example, most browser libraries / modules). For explanation see below:
Compression - Prototype Method
function Book(title) { this.title = title; } Book.prototype.getTitle = function () { return this.title; };
Is YUI compressed
function Book(a){this.title=a}Book.prototype.getTitle=function(){return this.title};
Save about 18% (all spaces / tabs / returns). This method requires the mapping of variables / functions (this.variable = value) so that each prototype function can access them. Therefore, these variables / functions cannot be optimized during compression.
Compression - Close Method
function Book(title) { var title = title;
Is YUI compressed
function Book(a){var a=a;this.getTitle=function(){return a}};
Save about 33%. Local variables can be optimized. In a large module with many auxiliary functions, this can have significant compression savings.
3. Project Management
In a project with several developers who can work on the same module, I prefer the Prototype Method for this module, if not limited to performance or compression.
For browser development, I can override producton.prototype.aFunction from "production.js" in my own "test.js" (read in words) for testing or development purposes without the need to modify the "production" .js ", which may be active developed by another developer.
I'm not a big fan of the complex GIT repository checkout / branch / merge / conflict flow. I prefer it simply.
In addition, the ability to override or "capture" a module function with a test node may be useful, but too complex to address here ...