0
Anything But Typical<br />Learning to Love JavaScript Prototypes<br />Page 1© 2010 Razorfish. All rights reserved.<br />Da...
JavaScript<br />Page 2© 2010 Razorfish. All rights reserved.<br />
Object-Oriented Programming<br />Page 3© 2010 Razorfish. All rights reserved.<br />
class<br />Page 4© 2010 Razorfish. All rights reserved.<br />
Inheritance<br />Page 5© 2010 Razorfish. All rights reserved.<br />
prototype(not the library)<br />Page 6© 2010 Razorfish. All rights reserved.<br />
The story so far…<br />Page 7© 2010 Razorfish. All rights reserved.<br />
<a onclick=“javascript:alert(‘chaos!’)”><br />Page 8© 2010 Razorfish. All rights reserved.<br />
Page 9© 2010 Razorfish. All rights reserved.<br />
…except it wasn’t all good.<br />Page 10© 2010 Razorfish. All rights reserved.<br />
Page 11© 2010 Razorfish. All rights reserved.<br />
Page 12© 2010 Razorfish. All rights reserved.<br />
Page 13© 2010 Razorfish. All rights reserved.<br />
Page 14© 2010 Razorfish. All rights reserved.<br />
Sound familiar?<br />Page 15© 2010 Razorfish. All rights reserved.<br />
Page 16© 2010 Razorfish. All rights reserved.<br />prototype(not the library) to the rescue!<br />
Page 17© 2010 Razorfish. All rights reserved.<br />Everything is an object<br />
Page 18© 2010 Razorfish. All rights reserved.<br />Every object has a prototype<br />
Page 19© 2010 Razorfish. All rights reserved.<br />prototypes do good things<br />
Page 20© 2010 Razorfish. All rights reserved.<br />Encourage modularityand reusability<br />
Page 21© 2010 Razorfish. All rights reserved.<br />Provide a consistent wayto do (different) things<br />
Page 22© 2010 Razorfish. All rights reserved.<br />Keep code to a minimum<br />
Page 23© 2010 Razorfish. All rights reserved.<br />Make complexity easy<br />
What is a prototype, exactly?<br />Page 24© 2010 Razorfish. All rights reserved.<br />
Page 25© 2010 Razorfish. All rights reserved.<br />A reference point for other objects<br />
Property lookup chain<br />Page 26© 2010 Razorfish. All rights reserved.<br />
Property lookup chain<br />Page 27© 2010 Razorfish. All rights reserved.<br />instance<br />Date<br />Object<br />
Property lookup chain<br />Page 28© 2010 Razorfish. All rights reserved.<br />instance<br />Date<br />Object<br />
Property lookup chain<br />Page 29© 2010 Razorfish. All rights reserved.<br />instance<br />Date<br />Object<br />
Property lookup chain<br />Page 30© 2010 Razorfish. All rights reserved.<br />instance<br />Date<br />Object<br />
Page 31© 2010 Razorfish. All rights reserved.<br />JavaScript is different frommost other OOP languages<br />
Anatomy of a prototype<br />Page 32© 2010 Razorfish. All rights reserved.<br />rex<br />Poodle<br />Dog<br />Object<br />
Anatomy of a prototype<br />Page 33© 2010 Razorfish. All rights reserved.<br />Constructor<br />
Anatomy of a prototype<br />Page 34© 2010 Razorfish. All rights reserved.<br />Property<br />Method<br />
Anatomy of a prototype<br />Page 35© 2010 Razorfish. All rights reserved.<br />Constructor<br />Inheritance<br />
Anatomy of a prototype<br />Page 36© 2010 Razorfish. All rights reserved.<br />Instance<br />Instance Property<br />
this and the lookup chain<br />Page 37© 2010 Razorfish. All rights reserved.<br />fluffy<br />socks<br />Cat<br />Object<b...
this and the lookup chain<br />Page 38© 2010 Razorfish. All rights reserved.<br />socks<br />Cat<br />Object<br />
this and the lookup chain<br />Page 39© 2010 Razorfish. All rights reserved.<br />fluffy<br />Cat<br />Object<br />
Execution context: this and call()<br />Page 40© 2010 Razorfish. All rights reserved.<br />
Execution context: this and call()<br />Page 41© 2010 Razorfish. All rights reserved.<br />
Execution context with instances<br />Page 42© 2010 Razorfish. All rights reserved.<br />Same outcome<br />
Page 43© 2010 Razorfish. All rights reserved.<br />Inheritance<br />
Working with inherited behavior<br />Page 44© 2010 Razorfish. All rights reserved.<br />LimitedContainer<br />Container<br...
Working with inherited behavior<br />Page 45© 2010 Razorfish. All rights reserved.<br />LimitedContainer<br />Container<br...
Working with inherited behavior<br />Page 46© 2010 Razorfish. All rights reserved.<br />LimitedContainer<br />Container<br...
Page 47© 2010 Razorfish. All rights reserved.<br />JavaScript is the Frankensteinof the OOP world<br />
Borrowing from multiple prototypes<br />Page 48© 2010 Razorfish. All rights reserved.<br />
Page 49© 2010 Razorfish. All rights reserved.<br />One small problem<br />
The constructor problem<br />Page 50© 2010 Razorfish. All rights reserved.<br />
The constructor problem<br />Page 51© 2010 Razorfish. All rights reserved.<br />
Fixing the constructor problem<br />Page 52© 2010 Razorfish. All rights reserved.<br />
Fixing the constructor problem<br />Page 53© 2010 Razorfish. All rights reserved.<br />
Page 54© 2010 Razorfish. All rights reserved.<br />prototype isn’t love at first sight<br />
Page 55© 2010 Razorfish. All rights reserved.<br />Putting prototypes to work<br />
Requirements<br /><ul><li> Four kinds of rules on the form:
Required
Email
Number
Zip Code
 Multiple rules can apply to one field
 Rules need to be inferred from  data-rich, standard HTML
Upcoming SlideShare
Loading in...5
×

Anything But Typical: Learning to Love JavaScript Prototypes

8,319

Published on

Frightened and confused by the term "prototype"? Tired of praying that there's some plugin for a framework that will do exactly what you need to do? Unlock a whole new world of interactivity with object-oriented JavaScript programming.

Published in: Business
3 Comments
16 Likes
Statistics
Notes
No Downloads
Views
Total Views
8,319
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
427
Comments
3
Likes
16
Embeds 0
No embeds

No notes for slide
  • Web was a scary placeBrowser compatibility and features all over the mapDevs hop from site to site grabbing snippets of code
  • JS libraries emerge several years agoSmooth over cross browser differencesMake complex behavior easyProvide frameworks for enhancement
  • New dependency on libraries and plugins is growingDevs spend a lot of time… (series)
  • Searching for a JS library extension that does what they need
  • Changing site to fit what the extension does
  • Brute-force coding requirements back in
  • Live in fear of repeating it allwhen make updateson the next project
  • One of JS most powerful featues goesUnusedIgnoredMisunderstoodAnd it can help
  • JS is OOP language
  • Objects are containers forProperties - DataMethods - BehaviorsWork with objects all the time
  • Template for properties + methodsWork with prototypes every dayEven if you don’t realize itDate, Array, etc
  • Code you writeCode loaded in memory
  • Easy to…BuildModifyExpand on complex interactions
  • Reference point that serves as basis for an objectTo understand prototypeNeed to understand property lookup chain
  • Simple exampleCreate a date var named “instance”Define a new property on the varCall several methods
  • PLC for instance var looks like thisInstance prototype = DateDate prototype = ObjectJS uses PLC to find values of referenced properties
  • Look up instance.fooJS finds it on the var itselfStops looking
  • Lookup instance.getTimeDoesn’t find it on the varFinds it on DateStops looking
  • Lookup instance.hasOwnPropertyDoesn’t find it on varDoesn’t find it on DateFinds it on Object
  • Prototypes are put together different than most
  • More complicated exampleRex is var instance of PoodlePoodle prototype = DogDog prototype = Object
  • Closer look at DogFunctionAll functions have prototype propertyPrototype defaults to ObjectConstructor is Function paired with NEW operatorUsed to create instances
  • Dog’s prototype property used to build the templateFunctions on a prototype are methodsNon-functions on a prototype are propertiesAnything defined on function’s prototype available to instances
  • Poodle is second typeFunction, just like DogInheritanceSet Poodle.prototype to new instance of DogCreates link between Poodle and Dog in PLCCreate properties on Poodle.prototypeAvailable to instances of Poodle
  • Rex is instance of PoodleProperty “name” defined on rex – instance propertyHas access to all of Poodle’s prototype propertiesAnd to other properties up PLC
  • Probably recognize THIS operatorLook at how THIS interacts with prototypeIn example:Socks and fluffy both instances of CatCat inherits from Object
  • When call socks.speakFinds speak property on Cat.prototypeInside of speak, encounter THISProperties on THIS walk PLC starting at instanceGreeting not a property of socks instanceGreeting is a property of Cat prototype
  • When call fluffy.speakSpeak function found on Cat.prototype, same as socksUnlike socks, fluffy has own greeting propertylookup stops at instance
  • Functions have call method that lets set execution contextTHIS operator refers back to execution context
  • When sayFoo called with exec context objTHIS = obj
  • Calling a method on an instance is same asUsing CALL to supply instance as exec context prototype method
  • Shown some basic examples so far, but don’t really take advantageDive deeper into inheritance
  • In example:Container type hasarray that it can store items inMethod to add itemsinherits from ObjectLimitedContainerInherits from ContainerChanges inherited behavior to limit how many items can be in array
  • Behavior in Container constructor creates items arrayTo get LC to also have that behaviorCALL Container constructor with THIS as exec context within LC constructor
  • Container’s addItem method just pushes into arrayWant to set a limit to how many items can be in arrayDefine new addItem method on LC prototypeThe new addItem is higher in the PLC, so it will be found before old oneAdd logic into new method to throw error if there are too many itemsCALL Container’s addItem method with THIS as exec context to get original behavior
  • Have kind of a patchwork natureCan take advantage of that to bring together lots of complicated behaviors
  • In example, CatDog borrows behavior from Cat and DogCat instances are furious when call swim methodDog instances are delightedCatDog is both delighted and furious when you call its swim method
  • May have noticed an issue in earlier examples
  • Have two types: Parent and ChildChild inherits from parent
  • When Child linked to ParentParent constructor is calledParent constructor has undesirable behaviorAlert goes off when set up inheritance
  • Get around issue with a simple helper function
  • Helper:Creates intermediate function that does nothingAssigns parent prototype to itInstantiates intermediate as prototypeBecause prototype is same b/t intermediate and parent, PLC stays intactAlso define constructor on child while we’re at itNo more undesirable behavior
  • Prototypes have never been accused of being very prettyBut they are very powerful
  • Up to now, haven’t had any real world examplesLet’s see what they can really doEXAMPLEPretty standard HTML formCommon problem is validating a formLet’s do it with prototypes!
  • Helper:Creates intermediate function that does nothingAssigns parent prototype to itInstantiates intermediate as prototypeBecause prototype is same b/t intermediate and parent, PLC stays intactAlso define constructor on child while we’re at itNo more undesirable behavior
  • Helper:Creates intermediate function that does nothingAssigns parent prototype to itInstantiates intermediate as prototypeBecause prototype is same b/t intermediate and parent, PLC stays intactAlso define constructor on child while we’re at itNo more undesirable behavior
  • And do a really good job of itStart to get into trouble when depend totally on the libraryStart to get into bad trouble when depending totally on plugins
  • Easy to…BuildModifyExpand on complex interactions
  • Help you design the interactionthe way it needs to workand in a reusable, extensible way that plays well with others
  • Any questions?
  • Transcript of "Anything But Typical: Learning to Love JavaScript Prototypes"

    1. 1. Anything But Typical<br />Learning to Love JavaScript Prototypes<br />Page 1© 2010 Razorfish. All rights reserved.<br />Dan Nichols<br />March 14, 2010<br />
    2. 2. JavaScript<br />Page 2© 2010 Razorfish. All rights reserved.<br />
    3. 3. Object-Oriented Programming<br />Page 3© 2010 Razorfish. All rights reserved.<br />
    4. 4. class<br />Page 4© 2010 Razorfish. All rights reserved.<br />
    5. 5. Inheritance<br />Page 5© 2010 Razorfish. All rights reserved.<br />
    6. 6. prototype(not the library)<br />Page 6© 2010 Razorfish. All rights reserved.<br />
    7. 7. The story so far…<br />Page 7© 2010 Razorfish. All rights reserved.<br />
    8. 8. <a onclick=“javascript:alert(‘chaos!’)”><br />Page 8© 2010 Razorfish. All rights reserved.<br />
    9. 9. Page 9© 2010 Razorfish. All rights reserved.<br />
    10. 10. …except it wasn’t all good.<br />Page 10© 2010 Razorfish. All rights reserved.<br />
    11. 11. Page 11© 2010 Razorfish. All rights reserved.<br />
    12. 12. Page 12© 2010 Razorfish. All rights reserved.<br />
    13. 13. Page 13© 2010 Razorfish. All rights reserved.<br />
    14. 14. Page 14© 2010 Razorfish. All rights reserved.<br />
    15. 15. Sound familiar?<br />Page 15© 2010 Razorfish. All rights reserved.<br />
    16. 16. Page 16© 2010 Razorfish. All rights reserved.<br />prototype(not the library) to the rescue!<br />
    17. 17. Page 17© 2010 Razorfish. All rights reserved.<br />Everything is an object<br />
    18. 18. Page 18© 2010 Razorfish. All rights reserved.<br />Every object has a prototype<br />
    19. 19. Page 19© 2010 Razorfish. All rights reserved.<br />prototypes do good things<br />
    20. 20. Page 20© 2010 Razorfish. All rights reserved.<br />Encourage modularityand reusability<br />
    21. 21. Page 21© 2010 Razorfish. All rights reserved.<br />Provide a consistent wayto do (different) things<br />
    22. 22. Page 22© 2010 Razorfish. All rights reserved.<br />Keep code to a minimum<br />
    23. 23. Page 23© 2010 Razorfish. All rights reserved.<br />Make complexity easy<br />
    24. 24. What is a prototype, exactly?<br />Page 24© 2010 Razorfish. All rights reserved.<br />
    25. 25. Page 25© 2010 Razorfish. All rights reserved.<br />A reference point for other objects<br />
    26. 26. Property lookup chain<br />Page 26© 2010 Razorfish. All rights reserved.<br />
    27. 27. Property lookup chain<br />Page 27© 2010 Razorfish. All rights reserved.<br />instance<br />Date<br />Object<br />
    28. 28. Property lookup chain<br />Page 28© 2010 Razorfish. All rights reserved.<br />instance<br />Date<br />Object<br />
    29. 29. Property lookup chain<br />Page 29© 2010 Razorfish. All rights reserved.<br />instance<br />Date<br />Object<br />
    30. 30. Property lookup chain<br />Page 30© 2010 Razorfish. All rights reserved.<br />instance<br />Date<br />Object<br />
    31. 31. Page 31© 2010 Razorfish. All rights reserved.<br />JavaScript is different frommost other OOP languages<br />
    32. 32. Anatomy of a prototype<br />Page 32© 2010 Razorfish. All rights reserved.<br />rex<br />Poodle<br />Dog<br />Object<br />
    33. 33. Anatomy of a prototype<br />Page 33© 2010 Razorfish. All rights reserved.<br />Constructor<br />
    34. 34. Anatomy of a prototype<br />Page 34© 2010 Razorfish. All rights reserved.<br />Property<br />Method<br />
    35. 35. Anatomy of a prototype<br />Page 35© 2010 Razorfish. All rights reserved.<br />Constructor<br />Inheritance<br />
    36. 36. Anatomy of a prototype<br />Page 36© 2010 Razorfish. All rights reserved.<br />Instance<br />Instance Property<br />
    37. 37. this and the lookup chain<br />Page 37© 2010 Razorfish. All rights reserved.<br />fluffy<br />socks<br />Cat<br />Object<br />
    38. 38. this and the lookup chain<br />Page 38© 2010 Razorfish. All rights reserved.<br />socks<br />Cat<br />Object<br />
    39. 39. this and the lookup chain<br />Page 39© 2010 Razorfish. All rights reserved.<br />fluffy<br />Cat<br />Object<br />
    40. 40. Execution context: this and call()<br />Page 40© 2010 Razorfish. All rights reserved.<br />
    41. 41. Execution context: this and call()<br />Page 41© 2010 Razorfish. All rights reserved.<br />
    42. 42. Execution context with instances<br />Page 42© 2010 Razorfish. All rights reserved.<br />Same outcome<br />
    43. 43. Page 43© 2010 Razorfish. All rights reserved.<br />Inheritance<br />
    44. 44. Working with inherited behavior<br />Page 44© 2010 Razorfish. All rights reserved.<br />LimitedContainer<br />Container<br />Object<br />
    45. 45. Working with inherited behavior<br />Page 45© 2010 Razorfish. All rights reserved.<br />LimitedContainer<br />Container<br />Object<br />
    46. 46. Working with inherited behavior<br />Page 46© 2010 Razorfish. All rights reserved.<br />LimitedContainer<br />Container<br />Object<br />
    47. 47. Page 47© 2010 Razorfish. All rights reserved.<br />JavaScript is the Frankensteinof the OOP world<br />
    48. 48. Borrowing from multiple prototypes<br />Page 48© 2010 Razorfish. All rights reserved.<br />
    49. 49. Page 49© 2010 Razorfish. All rights reserved.<br />One small problem<br />
    50. 50. The constructor problem<br />Page 50© 2010 Razorfish. All rights reserved.<br />
    51. 51. The constructor problem<br />Page 51© 2010 Razorfish. All rights reserved.<br />
    52. 52. Fixing the constructor problem<br />Page 52© 2010 Razorfish. All rights reserved.<br />
    53. 53. Fixing the constructor problem<br />Page 53© 2010 Razorfish. All rights reserved.<br />
    54. 54. Page 54© 2010 Razorfish. All rights reserved.<br />prototype isn’t love at first sight<br />
    55. 55. Page 55© 2010 Razorfish. All rights reserved.<br />Putting prototypes to work<br />
    56. 56. Requirements<br /><ul><li> Four kinds of rules on the form:
    57. 57. Required
    58. 58. Email
    59. 59. Number
    60. 60. Zip Code
    61. 61. Multiple rules can apply to one field
    62. 62. Rules need to be inferred from data-rich, standard HTML
    63. 63. Error messages should be specific
    64. 64. Flexible enough to be:
    65. 65. Usable for all other forms on the site
    66. 66. Extended for use in other projects</li></ul>Page 56© 2010 Razorfish. All rights reserved.<br />
    67. 67. The Plan<br /><ul><li>On load:</li></ul>Create validation rules for each field<br />Set up submit handler<br /><ul><li>When form is submitted:</li></ul>Cycle through all rules<br />If a field fails a rule<br />Mark it invalid<br />Create an error message for it<br />Don’t check any more rules for that field<br />Show all error messages<br />Page 57© 2010 Razorfish. All rights reserved.<br />
    68. 68. Page 58© 2010 Razorfish. All rights reserved.<br />NumberValidator<br />EmailValidator<br />RequiredValidator<br />PatternValidator<br />ValidatingForm<br />Validator<br />Object<br />
    69. 69. Page 59© 2010 Razorfish. All rights reserved.<br />Libraries are there to make life easier<br />
    70. 70. Page 60© 2010 Razorfish. All rights reserved.<br />Think about your approach<br />
    71. 71. Page 61© 2010 Razorfish. All rights reserved.<br />Dive in!<br />
    72. 72. Thank You<br />Dan Nichols<br />dan.nichols@razorfish.com<br />Blog: http://heydanno.com/blog<br />Code demo: http://heydanno.com/sxsw<br />Page 62© 2010 Razorfish. All rights reserved.<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×