What are the pros and cons of each type/approach in writing Object Oriented scripts?
Personally, I have found closures (functional? approach) as a way to encapsulate state more natural and perhaps more elegant. I, however, have heard that this use of closures is slower in JavaScript implementations.
I would at least like to know where a prototypal approach would be most appropriate.
“Functional” Style (most people would call this “traditional OOP”):
Prototype Style:
So if performance isn’t a huge issue for you and you’re only familiar with traditional OOP … go for it (Pro Javascript Design Patterns, from APress, has a good pattern for this). But if performance matters or you’re worried about the extra layer of abstraction complicating your debugging, take the time to read up on how prototype inheritance works; you’ll be a better Javascript programmer for it.
P.S. If you’re worried about not having true “private” methods with the prototype style, I strongly recommend reading:
http://snook.ca/archives/javascript/no-love-for-module-pattern
It provides a great explanation of why true “private” members are actually a bad thing (at least in most JS development environments).