Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Understanding JavaScript Prototypes

sporto
August 11, 2013

Understanding JavaScript Prototypes

sporto

August 11, 2013
Tweet

More Decks by sporto

Other Decks in Programming

Transcript

  1. The wrong way to learn function Liquid() { } Liquid.prototype.fluid

    = true; ! function Beer() { Liquid.call(this); } ! Beer.prototype = Object.create(Liquid.prototype); Beer.prototype.alcohol = 5.1; ! var beer = new Beer();
  2. • var liquid = {}
 var beer = {} •

    beer.__proto__ = liquid A simple chain
  3. Property look-ups • var liquid = {
 fluid: true
 }

    • var beer = {}
 beer.__proto__ = liquid • beer.fluid //-> true
  4. Long chain example • var liquid = { fluid: true

    } • var drink = {}
 drink.__proto__ = liquid • var beer = {}
 beer.__proto__ = drink • var coopers = {}
 coopers.__proto__ = beer • coopers.fluid; //-> true
  5. Prototype can be shared • var liquid = { fluid:

    true } • var beer = {}
 beer.__proto__ = liquid • var cider = {}
 cider.__proto__ = liquid • beer.fluid; //-> true
 cider.fluid; //-> true
  6. Prototypes are not copies • var beer = { tasty:

    true }; • var coopers = {};
 coopers.__proto__ = beer; • coopers.tasty; //-> true • beer.tasty = false; • cooper.tasty; //-> false
  7. Prototypes are dynamic • var beer = { alcohol: 6

    }
 var ethanol = { alcohol: 100 } • var coopers = {}
 coopers.__proto__ = beer • coopers.alcohol //-> 6 • coopers.__proto__ = ethanol • coopers.alcohol //-> 100
  8. Writing creates properties • var beer = { alcohol: 6

    } • var coopers = {}
 coopers.__proto__ = beer • coopers.alcohol = 4 • beer.alcohol //-> still 6
  9. beer coopers alcohol = 6 alcohol = 4 creatures Same

    property can be in several places
  10. Arrays • var beer = { 
 ingredients: [‘wheat’, ‘yeast’]

    
 } • var coopers = {}
 coopers.__proto__ = beer • coopers.ingredients.push(‘oats’) • beer.ingredients
 //->[‘wheat’, ‘yeast’, ‘oats’];
  11. coopers.ingredients.push(‘oats’); ! creatures.ingredients; //-> [‘wheat’, ‘yeast’, ‘oats’]; Arrays are modified

    directly in the prototype beer coopers creatures ingredients ['wheat', 'yeast'] __proto__ __proto__
  12. Objects are the same • var beer = { 


    ingredients: {wheat: 10, yeast: 20} 
 } • var coopers = {}
 coopers.__proto__ = beer • coopers.ingredients.wheat = 30 • beer.ingredients.wheat
 //-> 30
  13. Object.create • var beer = { 
 tasty: true
 }

    • var coopers = Object.create(beer) • coopers.tasty //-> true
  14. getPrototypeOf var beer = { tasty: true }; ! var

    coopers = Object.create(beer); ! ! Object.getPrototypeOf(coopers) //-> beer object
  15. isPrototypeOf var beer = { tasty: true }; ! var

    coopers = Object.create(beer); ! ! beer.isPrototypeOf(coopers); //-> true
  16. ES6 setPrototypeOf var beer = { tasty: true }; !

    var coopers = {}; ! ! Object.setPrototypeOf(coopers, beer);
  17. Functions as constructors function Beer(){ ! } ! var beer

    = new Beer(); Uppercase by convention
  18. function Beer(){ ! ! ! ! ! } ! var

    beer = new Beer(); Implicit this ! var this = {} this.__proto__ = Beer.prototype (yield to your code) return this
  19. Implicit this function Beer(){ var this = {}; this.__proto__ =

    Beer.prototype; this.tasty = true; return this; } ! var beer = new Beer(); ! beer.tasty; // true
  20. function Beer(){ this.tasty = true; } ! var beer =

    new Beer(); ! beer.tasty; //-> true Implicit this
  21. Don’t forget new function Beer(){ this.kind = ‘beer’; } !

    var beer = Beer(); this will the global object
  22. function.prototype function Beer(){ ! } ! var beer = new

    Beer(); beer.__proto__ ???? What is the __proto__ of beer?
  23. It is assigned as the prototype of the instance function

    Beer(){ var this = {}; this.__proto__ = Beer.prototype; this.tasty = true; return this; } It is assigned as the prototype of the new created object
  24. • function Beer(){} • Beer.prototype.tasty = true • var coopers

    = new Beer() • coopers.__proto__ == Beer.prototype
 //-> true • coopers.tasty //-> true It is the prototype assigned to the instance
  25. function Beer(){ } ! Beer.prototype.brew = function () {} !

    var coopers = new Beer(); var creatures = new Beer(); ! coopers.brew(); creatures.brew(); ! Useful for performance
  26. • function Beer(){...} • Beer.prototype.ingredients = ['honey'] • var coopers

    = new Beer()
 var vb = new Beer() • coopers.ingredients.push('oats') • vb.ingredients //-> ['honey', 'oats'] Again, same rules!
  27. • function Beer(){
 this.ingredients = [‘yeast’]
 } • var coopers

    = new Beer()
 var creatures = new Beer() • coopers.ingredients.push(‘wheat’) • creatures.ingredients
 //-> [‘yeast’] Isolation
  28. function Beer(){ ! } ! var coopers = new Beer();

    ! coopers instanceof Beer; //-> true ! instanceof
  29. var Beer = function () { ! }; ! Beer.findAll

    = function () { ... } ! Beer.findAll(); "Class methods"
  30. ES6

  31. ES6 Classes function Beer(a){ this.alcohol = a; } ! Beer.prototype.drink

    = function () { ... } ! var beer = new Beer(5) class Beer { constructor(a) { this.alcohol = a; } ! drink() { ... } } ! var beer = new Beer(5)