From my experiance, JavaScript does this:
- manipulating the DOM or other host objects
- adding event handlers
- doing Ajax
Since I started digging into prototypal inheritance, I would like to know how it is actually used in practice. What are the use cases?
Is anyone here actively using inheritance patterns? What for?
(I understand that there are lots of answers to my question – I just would like to hear some of them to get the feel of using inheritance in JavaScript)
My experience working with jQuery (and JavaScript before it) is that prototypical inheritance isn’t as useful as I was expecting. It has uses, but it’s not fundamentally important to the language.
In Javascript if you want to return an object with a method
foo:And callers can treat such objects as polymorphic – that is, they can call
foowithout worrying about what “type” of object it is. There is no need for inheritance.Against this background, prototypes are merely an optimisation. They allow you to create a large number of objects without having to replicate a big set of function properties in each instance. This is why jQuery uses it internally. A jQuery object has dozens of functions, and it might be a major overhead to copy them into every instance.
But from the perspective of a user of jQuery, prototypes aren’t particularly important. It could be rewritten to not use them and it would still work (but might use a lot more memory).