KEMBAR78
Object oriented javascript | KEY
Object-Oriented

Garrison Locke
•   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
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

• 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
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, undefined, and null

• Everything else is an object
   • Includes functions, objects, arrays,
     dates, regex, etc.
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
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.
Example Time!
• Encapsulation
• Logical decoupling
• Better code organization
• Prevent naming conflicts
• 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
Thanks!
    @gplocke


gplocke@gmail.com


http://joind.in/6512

Object oriented javascript

  • 1.
  • 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.
    Object-Orientedness • Encapsulation • Reusability •Decoupling • Responsibility Driven Development • Structure
  • 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.
    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!
  • 7.
    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.
  • 8.
    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
  • 9.
    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.
  • 10.
    Example Time! • Encapsulation •Logical decoupling • Better code organization • Prevent naming conflicts • Coding convention is super important!
  • 11.
    Object Decoupling • Howdo we effectively (and usefully) communicate between objects? • The Pubsub pattern to the rescue!
  • 12.
    Pubsub • Event drivendevelopment • Simple publish / subscription pattern • Basically a simplistic event manager for custom events • Kind of like the EventManager in ZF2
  • 13.
    Pubsub Pitfalls • Publisherassumes 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)
  • 14.
    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
  • 15.
    Thanks! @gplocke gplocke@gmail.com http://joind.in/6512

Editor's Notes