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

EKON 16: JavaScript

EKON 16: JavaScript

My slides for the JavaScript talk on EKON 16.

Sebastian Gingter

November 05, 2012
Tweet

More Decks by Sebastian Gingter

Other Decks in Technology

Transcript

  1. Sebastian P.R. Gingter - @PhoenixHawk EKON 16, November 2012 Slides:

    https://speakerdeck.com/u/phoenixhawk Sources: https://github.com/gingters/EKON16_JavaScript JavaScript
  2. EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk Nick

    Hodges „JavaScript is the new assembler of the browser.“
  3. EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk Some

    alternatives / extensions available Coffeescript, Dart, TypeScript
  4. JavaScript History EKON 16 - November 2012 Sebastian P.R. Gingter

    @PhoenixHawk •  1995 - First shipped as LiveScript in Netscape 2 •  A bit later renamed to JavaScript •  1996 – Used server-side in Netscape Enterprise Server •  1997 – Standardized as ECMAScript •  Late 90ies to mid 20ies (DHTML-Era) •  Considered ‚evil‘ •  Late 20ies: Renaissance with AJAX & Node.js
  5. JavaScript EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk

    •  Object-based •  Functional •  Prototypal •  Dynamic •  LISP-Like with C-Syntax •  Strongly depends on modules extending global scope
  6. Inject function EKON 16 - November 2012 Sebastian P.R. Gingter

    @PhoenixHawk var obj = { test: function(){ alert(‚Hallo‘); } }; obj._test = obj.test; obj.test = function() { this._test(); alert(‚Welt‘) }; obj.test();
  7. Structuring your JavaScript Code EKON 16 - November 2012 Sebastian

    P.R. Gingter @PhoenixHawk •  Credits go to Dan Wahlin & John Papa •  http://tinyurl.com/CleanJS
  8. Problems with function spaghetti EKON 16 - November 2012 Sebastian

    P.R. Gingter @PhoenixHawk •  Mixing of concerns •  No clear separation of functionality or purpose •  Variables/functions added into global scope •  Potential for duplicate function names •  Not modular •  Not easy to maintain •  No sense of a “container”
  9. Advantages of Ravioli code EKON 16 - November 2012 Sebastian

    P.R. Gingter @PhoenixHawk •  Objects encapsulate and decouple code •  Loosely coupled •  Separation of Concerns •  Variables/functions are scoped •  Functionality in closures •  Easier to maintain
  10. Non-Closure Example   function  myNonClosure()  {        

     var  date  =  new  Date();          return  date.getMilliseconds();   }   Variable lost after function returns EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk
  11. Closure Example   function  myClosure()  {        

     var  date  =  new  Date();            //nested  function            return  function  ()  {                  return  date.getMilliseconds();          };   }   Variable stays around even after function returns EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk
  12. Encapsulating Data with Object Literals •  Quick and easy • 

    All members are available •  Best suited for data var  calc  =  {          tempValue:  1,          add:  function(x,  y){                  return  x  +  y;          }   };     EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk
  13. Namespaces •  Encapsulate your code in a namespace: var  my

     =  my  ||  {};   EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk
  14. Using Namespaces my.calc  =  {          tempValue:

     1,          add:  function(x,  y)  {                  return  x  +  y;          }   };     EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk
  15. Prototype Pattern Structure var  Calculator  =  function(eq)  {    

         this.eqCtl  =  document.getElementById(eq);   };     Calculator.prototype  =  {          add:  function  (x,  y)  {                  var  val  =  x  +  y;                  this.eqCtl.innerHTML  =  val;          }   };   var calc = new Calculator('eqCtl'); calc.add(2,2); EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk
  16. Revealing Module Pattern Structure var  calculator  =  function(eq)  {  

         var  eqCtl  =  document.getElementById(eq),                              doAdd  =  function(x,y)  {                      var  val  =  x  +  y;                      eqCtl.innerHTML  =  val;                };                    return  {  add:  doAdd  };  //Expose  public  member   }('eqCtl');     calculator.add(2,2); EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk
  17. Revealing Prototype Pattern Structure var  Calculator  =  function  (eq)  {

             this.eqCtl  =  document.getElementById(eq);   };     Calculator.prototype  =  function()  {          var  doAdd  =  function  (x,  y)  {                  var  val  =  x  +  y;                  this.eqCtl.innerHTML  =  val;          };          return  {  add  :  doAdd  };   }();       var calc = new Calculator('eqCtl'); calc.add(2,2); EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk
  18. Summary •  Function spaghetti code is bad, Ravioli code is

    good •  Closures provide encapsulation •  Key patterns: •  Prototype Pattern •  Revealing Module Pattern •  Revealing Prototype Pattern EKON 16 - November 2012 Sebastian P.R. Gingter @PhoenixHawk