Practical example of need for prototypal inheritance
Alex Sexton wrote a wonderful article about how to use inheritance pattern to manage large piece of code. His code also has a pratical need for prototypal inheritance for writing modular code.
creating standard jQuery plugin
Given below is code that does exactly what Alex’s code does.
For smaller plugins this code is not too bad. However if the plugin is huge then it presents one big problem. The code for business problem and the code that deals with jQuery is all mixed in. What it means is that if tomorrow same functionality needs to be implemented for Prototype framework then it is not clear what part of code deals with framework and what part deals with business logic.
Separating business logic and framework code
Given below is code that separates business logic and framework code.
This code is an improvement over first iteration. However the whole business logic is captured inside a function. This code can be further improved by embracing object literal style of coding.
Third and final iteration of the code is the code presented by Alex.
Object.create is now part of ECMAScript 5 .