JS is problematic• Not *really* object-oriented• It has characteristics of OO languages• Multiple ways to do OOP in JS• Like PHP in that there are ﬁfty ways to accomplish a task• Also like PHP in that it’s easy to write completely functional spaghetti code that makes you want to claw your eyes out
Why OOJS?• JS isn’t just for image rollovers and fanciness anymore!• Applications are getting more complex and client-side intensive• Need for better code organization• Code reuse!
Objects in JS• 5 primitive data types: number, string, boolean, undeﬁned, and null• Everything else is an object • Includes functions, objects, arrays, dates, regex, etc.
Objects Continued...• Functions are ﬁrst-class citizens in JS• They can have member methods• Can be passed as values (anonymous functions)• Can act very object- oriented-y
Example Time!• Encapsulation• Logical decoupling• Better code organization• Prevent naming conﬂicts• Coding convention is super important!
Object Decoupling• How do we effectively (and usefully) communicate between objects?• The Pubsub pattern to the rescue!
Pubsub• Event driven development• Simple publish / subscription pattern• Basically a simplistic event manager for custom events • Kind of like the EventManager in ZF2
Pubsub Pitfalls• Publisher assumes subscriber is listening• Great for small to medium sized things, but can get unwieldy for very large projects• Overuse - use your best judgement (you don’t need to make everything a publisher)
Things to remember!• Keep it simple!• Pubsub is neat and convenient for decoupling!• Functions are objects and can contain variables and methods for maximum awesomeness!• Expose only your public methods via the