Object-OrientedGarrison Locke
•   Questions: ask ’em if you got    ’em•   There are many approaches to    this topic...this is just one way•   Follow al...
Object-Orientedness• Encapsulation• Reusability• Decoupling• Responsibility Driven Development• Structure
JS is problematic• Not *really* object-oriented• It has characteristics of OO languages• Multiple ways to do OOP in JS• Li...
Why OOJS?•   JS isn’t just for image    rollovers and fanciness    anymore!•   Applications are getting more    complex an...
Objects in JS• 5 primitive data types: number, string,  boolean, undefined, and null• Everything else is an object   • Incl...
Objects Continued...•   Functions are first-class    citizens in JS•   They can have member    methods•   Can be passed as ...
Approaches to OOJS• Object literals• “Functional” OOJS   • with or without self-invocation• Prototypical inheritance   • B...
Example Time!• Encapsulation• Logical decoupling• Better code organization• Prevent naming conflicts• Coding convention is ...
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 ...
Pubsub Pitfalls• Publisher assumes subscriber is  listening• Great for small to medium sized  things, but can get unwieldy...
Things to remember!• Keep it simple!• Pubsub is neat and convenient for  decoupling!• Functions are objects and can contai...
Thanks!    @gplockegplocke@gmail.comhttp://joind.in/6512
Object oriented javascript
Upcoming SlideShare
Loading in …5
×

Object oriented javascript

2,623 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,623
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
35
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • - You could accomplish the a similar thing with $.bind()\n
  • \n
  • \n
  • \n
  • Object oriented javascript

    1. 1. Object-OrientedGarrison Locke
    2. 2. • Questions: ask ’em if you got ’em• There are many approaches to this topic...this is just one way• Follow along with the examples!• github.com/gplocke/oojs- demo
    3. 3. Object-Orientedness• Encapsulation• Reusability• Decoupling• Responsibility Driven Development• Structure
    4. 4. 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 fifty 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
    5. 5. 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!
    6. 6. Objects in JS• 5 primitive data types: number, string, boolean, undefined, and null• Everything else is an object • Includes functions, objects, arrays, dates, regex, etc.
    7. 7. Objects Continued...• Functions are first-class citizens in JS• They can have member methods• Can be passed as values (anonymous functions)• Can act very object- oriented-y
    8. 8. Approaches to OOJS• Object literals• “Functional” OOJS • with or without self-invocation• Prototypical inheritance • Build upon built-in or user objects• Frameworks like Backbone.js, JavascriptMVC, ExtJS, etc.
    9. 9. Example Time!• Encapsulation• Logical decoupling• Better code organization• Prevent naming conflicts• Coding convention is super important!
    10. 10. Object Decoupling• How do we effectively (and usefully) communicate between objects?• The Pubsub pattern to the rescue!
    11. 11. Pubsub• Event driven development• Simple publish / subscription pattern• Basically a simplistic event manager for custom events • Kind of like the EventManager in ZF2
    12. 12. 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)
    13. 13. 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
    14. 14. Thanks! @gplockegplocke@gmail.comhttp://joind.in/6512

    ×