I just learned about prototype in Javascript (not the framework, the native feature). I perfectly got, at least one of its use. Example:
Array.prototype.myMethod = function(){
/*do something super awesome*/
}
var a = [];
a.myMethod();
Reading on I came upon an example in which the author adds a subClass method to the Object object, by doing this:
Object.subClass = function(hash){/*return an extended object that inherits Object's attributes*/}
The goal is to create a method that resembles a more object-oriented language syntax. Since I expected the book author to define such method using the prototype feature my question is:
- Why not use prototype?
- Isn't more risky to add methods directly to the object rather than the prototype attached to it?
- Are there situations in which I'd prefer one way over the other