Superclassy Inheritance In Javascript

  • 23,349 views
Uploaded on

Superclassy Inheritance with JavaScript is a quick look at the benefits and consequences of several inheritance patterns in JavaScript. Code reuse plays a major role in the DRY development pattern and …

Superclassy Inheritance with JavaScript is a quick look at the benefits and consequences of several inheritance patterns in JavaScript. Code reuse plays a major role in the DRY development pattern and leveraging the inheritance patterns built into JavaScript or manipulating them can change the way you build and organize large applications. Unfortunately, JavaScript's reputation and odd naming scheme have stopped people from using all the features that it has to offer. First, we'll discuss the array of options that exist and then go through a real-world example while using our newly honed inheritance-foo to make it play nice.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Javascript中的超类继承
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
23,349
On Slideshare
0
From Embeds
0
Number of Embeds
9

Actions

Shares
Downloads
167
Comments
1
Likes
13

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Superclassy Inheritance in Javascript Alex Sexton
  • 2. JavaScript Overview
    • Runs in more places than any other language.
  • 3. JavaScript Overview
    • Troubled by the DOM and Newbz
  • 4. JavaScript Overview
    • Simple but expressive
    • Functional by nature , but not by syntax
  • 5. JavaScript Overview
    • It _ is_ Object Oriented –
    • everything extends from Object
  • 6. Bad Javascript
    • Not Reusable/Modular
  • 7. Bad Javascript
    • Hard to read
      • (let’s leave nests to birds and aging mothers )
  • 8. Bad Javascript
    • Relies on the DOM (misusing jQuery, etc.)
  • 9.  
  • 10. Good JavaScript
    • Modular
    • Reusable
    • Readable
  • 11. Good JavaScript
    • Efficient
  • 12. Good JavaScript
    • Stays out of the DOM (for the most part)
  • 13.  
  • 14. Types of Inheritance in JS
    • Classical
    Prototypal
  • 15. Types of Inheritance in JS
    • Classical
    Prototypal Pseudo-Classical
  • 16. Types of Inheritance in JS
    • Classical
    Prototypal Pseudo-Classical ^----native (kind of) 
  • 17.
    • “ It doesn't matter what an object's lineage is, what matters is the quality of its contents .”
    • -Douglas Crockford
  • 18. Prototypal Inheritance
    • No Classes – just Objects
    • Objects inherit from Objects
  • 19. Prototypal Inheritance
    • Objects contain a “ link ” to another object
    • The “ parent ” Object is just a regular Object!
  • 20. Prototypal Inheritance
  • 21. Prototypal Lineage
    • Changes in oldObject bubble to newObject
    • Changes in newObject do not affect oldObject
  • 22. Prototypal Lineage
    • Theoretically can go on forever —
    • (you shouldn’t need more than a few levels)
    • Differential Inheritance is fast (common-case)
    • and memory efficient
  • 23. Pseudoclassical Inheritance
    • The syntax is Classical but behaves prototypally
  • 24. Pseudoclassical Inheritance
    • 3 Components to this pattern (the native pattern)
    • Constructor functions
    • ` new `
    • Function.prototype
  • 25. Pseudoclassical Inheritance
    • Classes in this pattern should be capitalized
    • Classes are named/defined by their
    • Constructor functions
  • 26. Pseudoclassical Inheritance
    • function Constructor (){
    • this.instance_member = …;
    • // automatically returns `this` when invoked
    • // with the `new` operator
    • }
    • var myObject = new Constructor();
  • 27. Pseudoclassical Inheritance
    • Methods and properties that are intended to be inherited are added to the function’s ‘ prototype ’
  • 28. Pseudoclassical Inheritance
    • The code inside the function is there to help ‘set up’ or ‘ construct ’ the returned object
    • function Constructor(val){ this.val = val; }
    • Constructor.prototype.method1 = function(){…};
    • Constructor.prototype.staticVal = 100;
    • var newObject = new Constructor(‘val’);
  • 29. Pseudoclassical Inheritance
    • The ‘ new ’ operator creates a new object, with a link back to its parent’s prototype object.
    • It is REQUIRED .
  • 30. Pseudoclassical Inheritance
    • This would be much like the prototypal
    • way of doing:
    • // prototypal equivalent
    • var newObject = Object.create(Constructor.prototype);
  • 31. Pseudoclassical Inheritance
    • // prototypal equivalent
    • var newObject = Object.create(Constructor.prototype);
  • 32. Classical Inheritance
    • At it’s core, Javascript is prototypal , and does not support the Classical model
  • 33. Classical Inheritance
    • Javascript is extremely versatile
    • Using a few simple tricks,
    • it’s easy to make believe
  • 34. Classical Inheritance
    • The ‘key’ is that any Objects ‘ prototype ’ member is mutable and can be replaced with another object.
    • This enables a new hierarchy.
  • 35. Purely Prototypal
    • Very short and easy to mimic in Javascript
    • Native function in ECMAscript 5 (approved)
    • Gets rid of ‘ new ’ and constructor functions
  • 36. Purely Prototypal
    • if (typeof Object.create !== 'function') {
    • Object.create = function (o) {
    • function F() {}
    • F.prototype = o;
    • return new F();
    • };
    • }
    • var newObject = Object.create(OldObject); //yay!
  • 37. Classical Inheritance in Javascript
    • A little tougher to implement, but there are plenty already written
    • simple-inheritance | MooTools Class | Prototype Class | Lowpro | Base2 |Way More…
  • 38. Classical Inheritance in Javascript
    • There is a small penalty in overhead since it’s not all native, but generally still fairly quick
    • Enhanced API gives easier access to the superclasses methods and often more
    • Easier than teaching old dogs new tricks
  • 39. simple-inheritance (api) by John Resig but pretty much stolen from Prototype and Base2
    • var  Person =  Class .extend ({   init:  function( isDancing ){      this .dancing = isDancing;    } ,   dance:  function(){      return   this .dancing;    }
    • }) ;
    • var  Ninja = Person.extend ({   init:  function(){      this ._super (   false   ) ; //  SEE?!!! SUPER CLASSY!    } ,   dance:  function(){      // Call the inherited version of dance()      return   this ._super () ;    } ,   swingSword:  function(){      return   true ;    }
    • }) ;
    • var  p =  new  Person (true) ;
    • p.dance () ;  // => true
    • var  n =  new  Ninja () ;
    • n.dance () ;  // => false
    • n.swingSword () ;  // => true
    • // Should all be true
    • p  instanceof  Person && p  instanceof   Class  &&
    • n  instanceof  Ninja && n  instanceof  Person && n  instanceof   Class
  • 40. Information Hiding
    • Use the “Module Pattern” – a function that takes advantage of closures and returns an object with access to private information that isn’t available outside of the function scope .
    • Or something…
  • 41. The Module Pattern
    • function module(){
    • var privateFunction = function() {
    • return “private”;
    • };
    • return {
    • publicFunction: function(){
    • return “public: “ + privateFunction();
    • }
    • };
    • }
    • var newModule = Object.create(module());
    • newModule.publicFunction(); // “public: private”
    • newModule.privateFunction(); // error
    • // Great for Getters and Setters of member values
  • 42.
    • “ But this doesn’t apply to my real life applications with X Library or Y Scripts!”
    • -Abe Lincoln
  • 43. Real Life
    • Rofl… that’s not a real quote guys…
    • It fits just fine. Stop trying to use the DOM as your point of access for everything .
  • 44. Real Life
    • Your library may actually address the issue –
    • or not …
  • 45.
    • “ I just think that the intersection between jQuery and classical inheritance is quite small.”
    • -John Resig
  • 46. Real Life
    • Build an API
    • Speakers should ‘ speak() ’ not append text to DOM elements
  • 47. A Badass Function
    • I like to call it “Stolen Identity” – it shows how versatile Javascript really is when it comes to inheritance
  • 48. A Badass Function
    • // example from Stoyan Stefanov for YUI inheritance
    • // Steal the member variables along with the prototype
    • function Programmer() {
    • // initialize the parent using the child as "this"
    • Programmer.superclass.constructor.apply(this, arguments);
    • }
  • 49. A Demo: Goals
    • Use inheritance(s) along with a framework
    • Tie the DOM and the Object together
    • in a clean manner
    • Pretty code!
  • 50.
    • (switch to codez)
  • 51. Fin.
    • Alex Sexton
    • [email_address]
    • http://twitter.com/slexaxton
    • http://alexsexton.com/inheritance/demo /
    • http:// yayquery.com /
    • Special Thanks to:
    • Douglas Crockford
    • John Resig
    • Stoyan Stefanov
    • Prototype | Base2 | Dojo | jQuery etc…
    • #jquery on irc.freenode.net
    • (YAYQUERY == Paul Irish, Rebecca Murphey, Adam Sontag)