After reading about both, I just have curiosity, how programming community uses this? In what situation which?
+3
A:
Prototype-based inheritance is more flexible. Any existing object can become a class from which additional objects will be spawned. This is handy where your objects offer several sets of services and/or they undergo a lot of state transformation before your program arrives at the point where the inheritance is needed.
A broad-spectrum discussion of modeling approaches is available here: http://steve-yegge.blogspot.com/2008/10/universal-design-pattern.html
Itay
2009-09-20 08:37:03
+1
A:
Since Javascript doesn't support "Classic" inheritance as most would understand (and you haven't given any references to what you've been reading) I'll assume you mean inheritance handled like this:-
function base() {
var myVar;
this.someBaseFunc = function() { }
}
function derived() {
base.call(this);
var someOtherVar;
this.SomeOtherFunc = function() { }
}
My general rule of thumb is:
- If the Classes are complex and the instances are few use a "classical" approach. This allows the classes to expose publicly only those features that should be made public.
- If the Class are simple and the instances are many use a prototypal approach. This limits the overhead of defining and storing references to functions over and over again as instances are created.
AnthonyWJones
2009-09-20 08:42:48