SlideShare a Scribd company logo
1 of 59
Download to read offline
Javascript
The Good Parts
      Federico Galassi             Gabriele Lana
federico.galassi@gmail.com   gabriele.lana@gmail.com
Why Javascript
 The language of the Web
 A nice, elegant, expressive
  language




                                2
Why Javascript
 A nice, elegant, expressive language...
   What??




                                        3
Javascript perceived as a toy language
 Good for small quick web hacks
 Not good for “serious” programming
   large projects
   mantainable code




                                       4
Javascript is a toy language? Bullshit!!
 Stanford University
  CS 242 “Programming Languages” 2008
 Mozilla Corporation
   Firefox
 Many more...




                                     5
Javascript is not toy language...So what?
 The Most Misunderstood language ever
   Different from mainstream
   Design mistakes
   Traditionally runs in the browser




                                        6
Javascript is different
 Not the language you already know
   You already know other good languages
   It’s a web designer thing, it’s “easy”
   Copy&Paste Oriented Programming




           !=
                                             7
Javascript is different
 Hidden nature
   Familiar syntax from Java/C
   Pretty exotic foundations
     Objects from Self
     Functions from Scheme

                if (x < 1) {
                    x++;
                }



                                  8
Javascript has design mistakes
 Short lab time
   hacked in one week in may 1995
   in netscape2 by the end of the year
 Too fast adoption
   web boom
 Controversial goals
   “easy” for non programmers
   must look like java
                                          Brendan Eich
 No fixes since 1999                     Creator of Javascript
                                          Mozilla CTO
                                               9
Javascript usually runs in the browser
 Inconsistent implementations
   poor specifications
 Depends on DOM for I/O
   and it sucks!
 Lack of common features
   file system access
   sockets
   “require”
 No standard libs

                                   10
Javascript rules indeed!
 Imperative
 Functional
 Object Oriented
 Simple
 Winner by natural selection
   ...where java failed.



                                Dachshund
                                Evolution

                                   11
Javascript can be made even better!
 Javascript has good and bad parts
 Use good parts the “right way”
 Cut bad parts
 ...Beautiful code                        AFTER




    BEFORE

                                      12
Javascript the different good parts:
Functions
 First class
      are objects
  
      can be created at runtime
  
      can be stored in variables
  
      can be passed as parameters to functions
  
      can be returned by functions
  
      can be expressed as anonymous literals
  




                                             13
Javascript the different good parts:
Working with Functions 1
     // Creation the old way
     function hello(name) {
         return quot;hello quot; + name;
     }

     // Creation the good way
     var hello = function(name) {
         return quot;hello quot; + name;
     };

     // Invocation
     hello;            // returns function()
     hello(quot;worldquot;);   // returns quot;hello worldquot;

     // Self Invocation
     (function(name) { return quot;hello quot; + name })(quot;worldquot;);


                                                  14
Javascript the different good parts:
Working with Functions 2
    // Passed as parameter and invoked
    var helloWorld = function() { print quot;hello world!quot; };
    var twice = function(func) {
        func(); func();
    };

    twice(helloWorld);   // prints quot;hello world!hello world!quot;

    // Returned by a function
    var makeHello = function() {
        return function(name) {
            return quot;hello quot; + name;
        }
    };

    var hello = makeHello();
    hello(quot;worldquot;);      // returns quot;hello worldquot;



                                                     15
Javascript the different good parts:
Functions and Scope
 Scopes                                          Global
                  var x = 1,                                 x=1
                      y = 2;                                 y=2
   Global                                                   v=3
                                            Function
                      function outer(p) {
   Function              var z = 3;
                          function inner() {
   No                                                       z=3
                              var x = 100,
    Block-level                                              p = ...
                                  w = 200;
 Call                    }

  Objects             }                                      x = 100
                                                             w = 200
                     if (true) {
 Scope                  var v = 3;
  Chain                                                     Scope Chain
                     }




                                                       16
Javascript the different good parts:
Functions as Closures 1
 Lexical Scoping
 Closures                                 Global        z = 100

                                       Function
              function outer() {
                  var z = 3;
                  return function() {
                             return z;                    z=3
                         }
              }

              var inner = outer();
              var z = 100;
                                             ion
                                      Execut
              inner(); // returns 3                     Scope Chain



                                                   17
Javascript the different good parts:
Functions as Closures 2
 Closures bind variables, not values !
// Wrong                        // Right
var funcs = [];                 var funcs = [];
for (var i = 0; i < 3; i++) {   for (var i = 0; i < 3; i++) {
    funcs[i] = function() {         funcs[i] = function(p) {
         return i;                    return function() {return p};
    }                               }(i)
}                               }

funcs[0](); // returns 3 !!     funcs[0](); // returns 0
funcs[1](); // returns 3 !!     funcs[1](); // returns 1
funcs[2](); // returns 3 !!     funcs[2](); // returns 2




                                                  18
Javascript the different good parts:
Objects 1
 Containers of key/value pairs (properties)
   keys are strings
   values are anything (loose typing)
                                 // Creation with literal
            book
                                 var book = {
      quot;titlequot;   quot;Javascriptquot;         title: quot;Javascriptquot;,
                                     pages: 240,
    quot;pagesquot;         240
                                     author: {
    quot;authorquot;         -
                                         name: quot;Federicoquot;,
                                         surname: quot;Galassiquot;
                                     }
                                 }
             quot;namequot; quot;Federicoquot;
           quot;surnamequot; quot;Galassiquot;
                                              19
Javascript the different good parts:
Objects 2
 Objects are dynamic
   Properties can be added and removed at runtime
   No class constraints
  // Get a property
  book[quot;titlequot;]           // returns quot;Javascriptquot;
  book.title              // same as book[quot;titlequot;]
  book.propertyNotThere   // returns undefined

  // Set or update a property
  book.cover = quot;butterfly.jpgquot;
  book.title = quot;Javascript the good partsquot;

  // Delete a property
  delete book.title       // now book.title is undefined


                                                     20
Javascript the different good parts:
Objects Methods
 Methods are                             book
  function valued                   quot;titlequot;    quot;Javascriptquot;
  properties                      quot;pagesquot;          240
                                   quot;readquot;           -
 Inside methods
                                                              Method
  this is bound to
                                  function() {
  object “on the left”                var action = quot;Reading quot;;
                                      return action + this.title;
book.read = function() {
                                  }
    var action = quot;Reading quot;;
    return action + this.title;
                                                                Scope
}
                                                  action = quot;Reading quot;
                                                  this   =
book.read(); // returns quot;Reading Javascriptquot;



                                                       21
Javascript the different good parts:
Objects Prototype
 Every object can be linked to another object
  through the prototype property (__proto__)
 If a property does not exist in the object, request is
  delegated to the prototype
                                     another_point
     var point = {
                                        quot;xquot;              20
         x: 10,
         y: 10                       __proto__            -
     };
     var another_point = {
         x: 20
                                                    point
     };
                                              quot;xquot;             10
     another_point.__proto__ = point;
     point.x; // returns 20                   quot;yquot;             10
     point.y; // returns 10 (delegated)
                                          __proto__            -
                                                    22
Javascript the different good parts:
Objects Prototype and Methods
 Delegation works for methods too
 this is always bound to the “first object”
   another_rect                        // returns 20 * 10 = 200
                                       another_rect.area();
   quot;widthquot;     20
                                                         Scope
 __proto__      -
                                                     this =
                          Prototype
              rect
                                                      Method
         quot;widthquot;     10
                                  function() {
        quot;heightquot;     10
                                      return this.width *
         quot;areaquot;       -                      this.height;
                                  }
       __proto__      -
                                                23
Javascript the different good parts:
Objects Prototype Chain
 Prototypes form a chain, followed to resolve
  properties
 At the end there is Object which provides common
  stuff, then undefined          first.asdasdasd;
                                          //   quot;asdasdasdquot; not in first,
       first                              //   second, last
                                          //   quot;asdasdasdquot; not in {}
 __proto__        -
                                          //   returns undefined

                             Pr luti
                              Re
                               op
                                 so
             second               er on
                                          first.hasOwnProperty
                                    ty    // returns function() ...
      __proto__          -

                                                    Object
                      last
             __proto__       -
                                                     24
Javascript the different good parts:
Working with Objects and Prototypes 1
       var car = {
           color: quot;whitequot;,
           speed: 0,
           accel: function(kmh) {
               this.speed += kmh;
           }
       };

       // Make a supercar from car
       var supercar = {
           color: quot;blackquot;,
           __proto__: car
       };

       // or also by cloning
       var supercar = Object.create(car, {
           color: quot;blackquot;
       });
                                             25
Javascript the different good parts:
Working with Objects and Prototypes 2
       // Prototype relationship is dynamic
       car.wheels = 4;
       supercar.wheels; // 4

       // Setting properties is local
       supercar.driver = quot;M. Knightquot;;
       car.driver; // undefined

       // Where properties are from?
       quot;driverquot; in supercar; // true
       quot;wheelsquot; in supercar; // true
       supercar.hasOwnProperty(quot;driverquot;); // true
       supercar.hasOwnProperty(quot;wheelsquot;); // false

       // Common to all objects
       supercar.toString();         // quot;[object Object]quot;
       car.isPrototypeOf(supercar); // true

                                               26
Javascript the different good parts:
Objects Prototypal Inheritance 1
 Prototypes are javascript way to share
   Data
   Behaviour




                                       27
Javascript the different good parts:
Objects Prototypal Inheritance 2
 Prototypal Inheritance
   Vs Classical Inheritance
   Simpler
      No classes and objects, only objects
   Easier
      Work by examples, not abstractions
   Powerful !!
      Can simulate classical
      Reverse not true
   Shhhh, Don’t tell anyone
      Easier to write spaghetti code

                                              28
Javascript the different good parts:
Objects Prototypal Inheritance 3
 Ok, I cheated
   __proto__ available in mozilla only
   Object.create coming in next revision of language
 Javascript is schizophrenic
   Prototypal nature
   Wannabe classical




                                            29
Javascript the different good parts:
Objects Constructor Functions 1
 Constructor Functions
   Boring
   Function has a “prototype” property
   The “prototype” property has a
    “constructor” property which points
    back to the Function object
   Function can be invoked with the
    “new” operator
   Create an object whose __proto__
    is “prototype” property of Function
   Initialize the object executing Function

                                               30
Javascript the different good parts:
Objects Constructor Functions 2
// Constructor function       // silently executes
function Dog(name) {          // Dog.prototype = { constructor: Dog }
    this.name = name;
}

Dog.prototype;                // Object
Dog.prototype.constructor;    // return function Dog

// Create a new Dog object // silently executes
var fido = new Dog(quot;fidoquot;); // var fido = Object.create(Dog.prototype);
                            // Dog.call(fido, quot;fidoquot;);

fido.__proto__;               //   Object
fido.__proto__.constructor;   //   Dog
fido.constructor;             //   Dog (inherited by __proto__)
fido.name                     //   fido


                                                       31
Javascript the different good parts:
Objects Constructor Functions 3
                                        function Rectangle(w, h) {
 Why?                                      this.w = w;
      Function is a constructor
                                           this.h = h;
                                        }
      Function prototype is a class
                                       Rectangle.prototype.higher =
                                        function() { this.h += 1 };
      new is new
  
      Feels classical, Feels familiar
                                       var rect = new Rectangle(5,10);

 Worst of both worlds
   Unnecessarily complicated
   Hide prototypal nature
   Weird for classical programmers


                                                    32
Javascript the different good parts:
Objects Constructor Functions Fix
 Fortunately there’s a Quick Fix

      // waiting for next javascript implementation...

      if (typeof Object.create !== 'function') {
          Object.create = function (o) {
              var F = function() {};
              F.prototype = o;
              return new F();
          };
      }




                                                   33
Javascript the different good parts:
Arrays
 No real arrays in javascript
 They’re objects in disguise
   special props and methods
 Cool literal syntax




                                   34
Javascript the different good parts:
Arrays Example 1
  // array literal
  var numbers = [1, 2, 3, 4, 5];

  // reference
  numbers[2]; // returns 3
  numbers[10]; // returns undefined

  // length property
  numbers.length;      // 5

  // nice methods
  numbers.push(11);    // now [1, 2, 3, 4, 5, 11]
  numbers.join(quot; quot;);   // returns quot;1 2 3 4 5 11quot;




                                                    35
Javascript the different good parts:
Arrays Example 2
  // actually...

  // indexes are just object properties
  numbers.2; // would return 3

  // common object methods
  numbers.hasOwnProperty(0);   // returns true

  // length = last numeric prop + 1
  numbers[100] = 100;
  numbers.length;     // returns 101

  // ultimate proof
  typeof numbers;     // returns quot;objectquot;




                                                 36
Javascript the different good parts:
Functional Programming
 Iterators
 Callbacks
 Module Pattern
 Curry
 Memoization




                                   37
Javascript the different good parts:
Functional Programming Iterators
 Take control of loops
 Reduce accidental complexity




                                   38
Javascript the different good parts:
Iterators Example 1
  // iterate on a collection

  function each(arr, func) {
      for (var i=0; i<arr.length; i++) {
          func(arr[i]);
      }
  }

  var ten = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

  each(ten, function(i) {
      print i;
  });
  // prints 12345678910




                                               39
Javascript the different good parts:
Iterators Example 2
   // maps a collection to a new one

   function map(arr, func) {
       var result = [];
       each(arr, function(i) {
           result.push(func(i));
       });
       return result;
   }

   var ten = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

   map(ten, function(i) {
       return i * i;
   });
   // returns [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


                                                  40
Javascript the different good parts:
Iterators Example 3
   // filter elements of a collection

   function filter(arr, func) {
       var result = [];
       each(arr, function(i) {
           if (func(i)) { result.push(i); }
       });
       return result;
   }

   var ten = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

   filter(ten, function(i) {
       return i % 2 === 0;
   });
   // returns [2, 4, 6, 8, 10]


                                                41
Javascript the different good parts:
Iterators Example 4
  // compute a single value from a collection

  function reduce(arr, func, start) {
      var result = start;
      each(arr, function(i) {
          result = func(i, result);
      });
      return result;
  }

  var ten = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

  reduce(ten, function(i, sum) {
      return i + sum;
  });
  // returns 55


                                                42
Javascript the different good parts:
Iterators Example 5
 // Composability
 // square elements, then pick even ones, then sum

 reduce(
     filter(
         map(ten,
             function(i) { return i * i; }
         ),
         function(i) { return i % 2 === 0; }
     ),
     function(i, sum) { return i + sum; },
     0
 );




                                                     43
Javascript the different good parts:
Iterators Example 6
 // Composability but easy to read

 var square = function(arr) {
     return map(arr, function(i) { return i * 2; });
 }
 var even = function(arr) {
     return filter(arr, function(i) { return i % 2 === 0; });
 }
 var sum = function(arr) {
     return reduce(arr, function(i, total) { return i + total; }, 0);
 }

 sum(even(square(ten)));




                                                   44
Javascript the different good parts:
Functional Programming Callbacks
 Manage asynchronous communication
 Hide complexity




                                   45
Javascript the different good parts:
Callbacks Example 1
 // Synchronous request

 var response = get(quot;http://www.google.comquot;);
 display(response);



 // Asynchronous with callback

 get(quot;http://www.google.comquot;, function(response) {
     display(response);
 });




                                                     46
Javascript the different good parts:
Callbacks Example 2
 // explicit complexity
 var response = get(quot;http://www.google.comquot;);
 if (response.completed) {
     if (response.httpCode === quot;200quot;) {
        display(response);
     } else {
          // http error
     }
 } else {
     // network error
 }




                                                47
Javascript the different good parts:
Callbacks Example 3
 // complexity hidden in the client code
 var response = get(quot;http://www.google.comquot;);
 if (success(response)) {
     display(response);
 } else {
     // error
 }

 // complexity hidden away
 get(quot;http://www.google.comquot;, {
     success: function(response) {
         display(response);
     }
 });




                                                48
Javascript the different good parts:
Functional Programming Module Pattern
 Hide state and behaviour




                                49
Javascript the different good parts:
Module Pattern Example 1
var numbers = [quot;zeroquot;, quot;onequot;, quot;twoquot;, quot;threequot;, ...];   // GLOBAL BAD
var numberToString = function(num) {
    return numbers[num];
}

var numberToString = function(num) {                // LOCAL SLOW
    var numbers = [quot;zeroquot;, quot;onequot;, quot;twoquot;, quot;threequot;, ...];
    return numbers[num];
}

var numberToString = function() {                  // PRIVATE AND FAST
     var numbers = [quot;zeroquot;, quot;onequot;, quot;twoquot;, quot;threequot;, ...];
     return function(num) {
         return numbers[num];
    }
}();


                                                      50
Javascript the different good parts:
Functional Programming Memoization
 Cache computation
 Speed up execution




                                 51
Javascript the different good parts:
Memoization Example 1
 // get pixels. maybe millions of them
 var pixels = getImagePixels(quot;image.jpgquot;);

 var getColor = function(pixel) {
     // ... computation on RGB values ...
     returns quot;blackquot;; // or quot;whitequot; or quot;greenquot; etc...
 }

 // find the color
 pixels.each(function(pixel) {
     var color = getColor(pixel);
     // store result
 });




                                                   52
Javascript the different good parts:
Memoization Example 2
 // wasted computation, cache it...
 var getColorCache = function(func) {
     var cache;
     // setup cache ...
     return function(pixel) {
         if (cache.missing(pixel)) {
              cache.store(pixel, func(pixel));
         }
         return cache.get(pixel);
     }
 }(getColor);




                                                 53
Javascript the bad parts
 The features you should definitely avoid




                                        54
Javascript the bad parts 1
 Global variables
 Semicolon insertion
// Good, returns { ok: true }   // Very bad, returns undefined
return {                        return
    ok: true                    {
}                                   ok: true
                                }

 Reserved words
// Good                         // Very bad, error
book[quot;classquot;];                  book.class;
var book = {                    var book = {
    quot;classquot;: quot;bookquot;                 class: quot;bookquot;
}                               }


                                               55
Javascript the bad parts 2
 Unicode
 typeof
  // Not useful, returns quot;objectquot;
  typeof array;
  // Wrong, returns quot;objectquot;
  typeof null;
  // Inconsistent, returns quot;functionquot; or quot;objectquot;
  typeof /a/;

 parseInt
  // Good, returns 8                 // Wrong, returns 0
  parseInt(quot;08quot;, 10);                parseInt(quot;08quot;);




                                                    56
Javascript the bad parts 3
+
 Floating Point
  0.2 + 0.1 === 0.3   // false

 Phony Arrays
  arguments.join // returns undefined

 Falsy values
  if (book.name == null) { ... // 2 errors, works by coincidence

 == type coercion
       '' == '0' // false        false == undefined // false
       0 == '' // true           false == null      // false
       0 == '0' // true          null == undefined // true

                                                      57
Javascript the bad parts 4
 Objects are not hashes
              var cache;
              var word = getWord();   // returns quot;constructorquot;
              if (word in cache) {    // ops, true

 Deep for..in
              // safe way
              for (var i in list) {
                  if (list.hasOwnProperty(i)) {
                      // do something
                  }
              }
 Extending native prototypes kill kittens
   Prototype library mess


                                                  58
Credits
 Thank to Douglas Crockford and his book
 He knows the way




                                     59

More Related Content

What's hot

Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Aaron Gustafson
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To ClosureRobert Nyman
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScriptT11 Sessions
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesDoris Chen
 
Why Every Tester Should Learn Ruby
Why Every Tester Should Learn RubyWhy Every Tester Should Learn Ruby
Why Every Tester Should Learn RubyRaimonds Simanovskis
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101ygv2000
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesAnkit Rastogi
 
LinkedIn TBC JavaScript 100: Intro
LinkedIn TBC JavaScript 100: IntroLinkedIn TBC JavaScript 100: Intro
LinkedIn TBC JavaScript 100: IntroAdam Crabtree
 
Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Jaroslaw Palka
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testingVikas Thange
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript TutorialBui Kiet
 
Intro to Cocoa KVC/KVO and Bindings
Intro to Cocoa KVC/KVO and BindingsIntro to Cocoa KVC/KVO and Bindings
Intro to Cocoa KVC/KVO and BindingsSergio Acosta
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScriptChengHui Weng
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of JavascriptTarek Yehia
 

What's hot (20)

Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Rails on Oracle 2011
Rails on Oracle 2011Rails on Oracle 2011
Rails on Oracle 2011
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To Closure
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Java script for web developer
Java script for web developerJava script for web developer
Java script for web developer
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 
Why Every Tester Should Learn Ruby
Why Every Tester Should Learn RubyWhy Every Tester Should Learn Ruby
Why Every Tester Should Learn Ruby
 
Java script
Java scriptJava script
Java script
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
LinkedIn TBC JavaScript 100: Intro
LinkedIn TBC JavaScript 100: IntroLinkedIn TBC JavaScript 100: Intro
LinkedIn TBC JavaScript 100: Intro
 
Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testing
 
Java script
Java scriptJava script
Java script
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Intro to Cocoa KVC/KVO and Bindings
Intro to Cocoa KVC/KVO and BindingsIntro to Cocoa KVC/KVO and Bindings
Intro to Cocoa KVC/KVO and Bindings
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
 
Es.next
Es.nextEs.next
Es.next
 

Similar to Javascript: The Good Parts in 40 Characters

LISA Qooxdoo Tutorial Handouts
LISA Qooxdoo Tutorial HandoutsLISA Qooxdoo Tutorial Handouts
LISA Qooxdoo Tutorial HandoutsTobias Oetiker
 
JavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuanJavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuanWei-Yuan Chang
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsAjax Experience 2009
 
LISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesLISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesTobias Oetiker
 
javascript teach
javascript teachjavascript teach
javascript teachguest3732fa
 
JSBootcamp_White
JSBootcamp_WhiteJSBootcamp_White
JSBootcamp_Whiteguest3732fa
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Railselliando dias
 
Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....Raffi Krikorian
 
WDB005.1 - JavaScript for Java Developers (Lecture 1)
WDB005.1 - JavaScript for Java Developers (Lecture 1)WDB005.1 - JavaScript for Java Developers (Lecture 1)
WDB005.1 - JavaScript for Java Developers (Lecture 1)Igor Khotin
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developersAndrew Eddie
 
Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2rubyMarc Chung
 
Douglas Crockford: Serversideness
Douglas Crockford: ServersidenessDouglas Crockford: Serversideness
Douglas Crockford: ServersidenessWebExpo
 
WebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webWebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webpjcozzi
 
CoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayCoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayLim Chanmann
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasminePaulo Ragonha
 

Similar to Javascript: The Good Parts in 40 Characters (20)

LISA Qooxdoo Tutorial Handouts
LISA Qooxdoo Tutorial HandoutsLISA Qooxdoo Tutorial Handouts
LISA Qooxdoo Tutorial Handouts
 
JavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuanJavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuan
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
 
Goodparts
GoodpartsGoodparts
Goodparts
 
LISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesLISA QooxdooTutorial Slides
LISA QooxdooTutorial Slides
 
Javascript status 2016
Javascript status 2016Javascript status 2016
Javascript status 2016
 
javascript teach
javascript teachjavascript teach
javascript teach
 
JSBootcamp_White
JSBootcamp_WhiteJSBootcamp_White
JSBootcamp_White
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....
 
WDB005.1 - JavaScript for Java Developers (Lecture 1)
WDB005.1 - JavaScript for Java Developers (Lecture 1)WDB005.1 - JavaScript for Java Developers (Lecture 1)
WDB005.1 - JavaScript for Java Developers (Lecture 1)
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developers
 
ES6: The Awesome Parts
ES6: The Awesome PartsES6: The Awesome Parts
ES6: The Awesome Parts
 
Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2ruby
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
Douglas Crockford: Serversideness
Douglas Crockford: ServersidenessDouglas Crockford: Serversideness
Douglas Crockford: Serversideness
 
WebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webWebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open web
 
CoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayCoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple way
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 

More from Federico Galassi

Vim, the Way of the Keyboard
Vim, the Way of the KeyboardVim, the Way of the Keyboard
Vim, the Way of the KeyboardFederico Galassi
 
The Strange World of Javascript and all its little Asynchronous Beasts
The Strange World of Javascript and all its little Asynchronous BeastsThe Strange World of Javascript and all its little Asynchronous Beasts
The Strange World of Javascript and all its little Asynchronous BeastsFederico Galassi
 
Javascript the New Parts v2
Javascript the New Parts v2Javascript the New Parts v2
Javascript the New Parts v2Federico Galassi
 
CouchApps: Requiem for Accidental Complexity
CouchApps: Requiem for Accidental ComplexityCouchApps: Requiem for Accidental Complexity
CouchApps: Requiem for Accidental ComplexityFederico Galassi
 
Please Don't Touch the Slow Parts V3
Please Don't Touch the Slow Parts V3Please Don't Touch the Slow Parts V3
Please Don't Touch the Slow Parts V3Federico Galassi
 
Please Don't Touch the Slow Parts V2
Please Don't Touch the Slow Parts V2Please Don't Touch the Slow Parts V2
Please Don't Touch the Slow Parts V2Federico Galassi
 
Please Don't Touch the Slow Parts
Please Don't Touch the Slow PartsPlease Don't Touch the Slow Parts
Please Don't Touch the Slow PartsFederico Galassi
 

More from Federico Galassi (9)

Vim, the Way of the Keyboard
Vim, the Way of the KeyboardVim, the Way of the Keyboard
Vim, the Way of the Keyboard
 
The Strange World of Javascript and all its little Asynchronous Beasts
The Strange World of Javascript and all its little Asynchronous BeastsThe Strange World of Javascript and all its little Asynchronous Beasts
The Strange World of Javascript and all its little Asynchronous Beasts
 
Javascript the New Parts v2
Javascript the New Parts v2Javascript the New Parts v2
Javascript the New Parts v2
 
CouchApps: Requiem for Accidental Complexity
CouchApps: Requiem for Accidental ComplexityCouchApps: Requiem for Accidental Complexity
CouchApps: Requiem for Accidental Complexity
 
Please Don't Touch the Slow Parts V3
Please Don't Touch the Slow Parts V3Please Don't Touch the Slow Parts V3
Please Don't Touch the Slow Parts V3
 
Javascript the New Parts
Javascript the New PartsJavascript the New Parts
Javascript the New Parts
 
Event Driven Javascript
Event Driven JavascriptEvent Driven Javascript
Event Driven Javascript
 
Please Don't Touch the Slow Parts V2
Please Don't Touch the Slow Parts V2Please Don't Touch the Slow Parts V2
Please Don't Touch the Slow Parts V2
 
Please Don't Touch the Slow Parts
Please Don't Touch the Slow PartsPlease Don't Touch the Slow Parts
Please Don't Touch the Slow Parts
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

Javascript: The Good Parts in 40 Characters

  • 1. Javascript The Good Parts Federico Galassi Gabriele Lana federico.galassi@gmail.com gabriele.lana@gmail.com
  • 2. Why Javascript  The language of the Web  A nice, elegant, expressive language 2
  • 3. Why Javascript  A nice, elegant, expressive language...  What?? 3
  • 4. Javascript perceived as a toy language  Good for small quick web hacks  Not good for “serious” programming  large projects  mantainable code 4
  • 5. Javascript is a toy language? Bullshit!!  Stanford University CS 242 “Programming Languages” 2008  Mozilla Corporation  Firefox  Many more... 5
  • 6. Javascript is not toy language...So what?  The Most Misunderstood language ever  Different from mainstream  Design mistakes  Traditionally runs in the browser 6
  • 7. Javascript is different  Not the language you already know  You already know other good languages  It’s a web designer thing, it’s “easy”  Copy&Paste Oriented Programming != 7
  • 8. Javascript is different  Hidden nature  Familiar syntax from Java/C  Pretty exotic foundations  Objects from Self  Functions from Scheme if (x < 1) { x++; } 8
  • 9. Javascript has design mistakes  Short lab time  hacked in one week in may 1995  in netscape2 by the end of the year  Too fast adoption  web boom  Controversial goals  “easy” for non programmers  must look like java Brendan Eich  No fixes since 1999 Creator of Javascript Mozilla CTO 9
  • 10. Javascript usually runs in the browser  Inconsistent implementations  poor specifications  Depends on DOM for I/O  and it sucks!  Lack of common features  file system access  sockets  “require”  No standard libs 10
  • 11. Javascript rules indeed!  Imperative  Functional  Object Oriented  Simple  Winner by natural selection  ...where java failed. Dachshund Evolution 11
  • 12. Javascript can be made even better!  Javascript has good and bad parts  Use good parts the “right way”  Cut bad parts  ...Beautiful code AFTER BEFORE 12
  • 13. Javascript the different good parts: Functions  First class are objects  can be created at runtime  can be stored in variables  can be passed as parameters to functions  can be returned by functions  can be expressed as anonymous literals  13
  • 14. Javascript the different good parts: Working with Functions 1 // Creation the old way function hello(name) { return quot;hello quot; + name; } // Creation the good way var hello = function(name) { return quot;hello quot; + name; }; // Invocation hello; // returns function() hello(quot;worldquot;); // returns quot;hello worldquot; // Self Invocation (function(name) { return quot;hello quot; + name })(quot;worldquot;); 14
  • 15. Javascript the different good parts: Working with Functions 2 // Passed as parameter and invoked var helloWorld = function() { print quot;hello world!quot; }; var twice = function(func) { func(); func(); }; twice(helloWorld); // prints quot;hello world!hello world!quot; // Returned by a function var makeHello = function() { return function(name) { return quot;hello quot; + name; } }; var hello = makeHello(); hello(quot;worldquot;); // returns quot;hello worldquot; 15
  • 16. Javascript the different good parts: Functions and Scope  Scopes Global var x = 1, x=1 y = 2; y=2  Global v=3 Function function outer(p) {  Function var z = 3; function inner() {  No z=3 var x = 100, Block-level p = ... w = 200;  Call } Objects } x = 100 w = 200 if (true) {  Scope var v = 3; Chain Scope Chain } 16
  • 17. Javascript the different good parts: Functions as Closures 1  Lexical Scoping  Closures Global z = 100 Function function outer() { var z = 3; return function() { return z; z=3 } } var inner = outer(); var z = 100; ion Execut inner(); // returns 3 Scope Chain 17
  • 18. Javascript the different good parts: Functions as Closures 2  Closures bind variables, not values ! // Wrong // Right var funcs = []; var funcs = []; for (var i = 0; i < 3; i++) { for (var i = 0; i < 3; i++) { funcs[i] = function() { funcs[i] = function(p) { return i; return function() {return p}; } }(i) } } funcs[0](); // returns 3 !! funcs[0](); // returns 0 funcs[1](); // returns 3 !! funcs[1](); // returns 1 funcs[2](); // returns 3 !! funcs[2](); // returns 2 18
  • 19. Javascript the different good parts: Objects 1  Containers of key/value pairs (properties)  keys are strings  values are anything (loose typing) // Creation with literal book var book = { quot;titlequot; quot;Javascriptquot; title: quot;Javascriptquot;, pages: 240, quot;pagesquot; 240 author: { quot;authorquot; - name: quot;Federicoquot;, surname: quot;Galassiquot; } } quot;namequot; quot;Federicoquot; quot;surnamequot; quot;Galassiquot; 19
  • 20. Javascript the different good parts: Objects 2  Objects are dynamic  Properties can be added and removed at runtime  No class constraints // Get a property book[quot;titlequot;] // returns quot;Javascriptquot; book.title // same as book[quot;titlequot;] book.propertyNotThere // returns undefined // Set or update a property book.cover = quot;butterfly.jpgquot; book.title = quot;Javascript the good partsquot; // Delete a property delete book.title // now book.title is undefined 20
  • 21. Javascript the different good parts: Objects Methods  Methods are book function valued quot;titlequot; quot;Javascriptquot; properties quot;pagesquot; 240 quot;readquot; -  Inside methods Method this is bound to function() { object “on the left” var action = quot;Reading quot;; return action + this.title; book.read = function() { } var action = quot;Reading quot;; return action + this.title; Scope } action = quot;Reading quot; this = book.read(); // returns quot;Reading Javascriptquot; 21
  • 22. Javascript the different good parts: Objects Prototype  Every object can be linked to another object through the prototype property (__proto__)  If a property does not exist in the object, request is delegated to the prototype another_point var point = { quot;xquot; 20 x: 10, y: 10 __proto__ - }; var another_point = { x: 20 point }; quot;xquot; 10 another_point.__proto__ = point; point.x; // returns 20 quot;yquot; 10 point.y; // returns 10 (delegated) __proto__ - 22
  • 23. Javascript the different good parts: Objects Prototype and Methods  Delegation works for methods too  this is always bound to the “first object” another_rect // returns 20 * 10 = 200 another_rect.area(); quot;widthquot; 20 Scope __proto__ - this = Prototype rect Method quot;widthquot; 10 function() { quot;heightquot; 10 return this.width * quot;areaquot; - this.height; } __proto__ - 23
  • 24. Javascript the different good parts: Objects Prototype Chain  Prototypes form a chain, followed to resolve properties  At the end there is Object which provides common stuff, then undefined first.asdasdasd; // quot;asdasdasdquot; not in first, first // second, last // quot;asdasdasdquot; not in {} __proto__ - // returns undefined Pr luti Re op so second er on first.hasOwnProperty ty // returns function() ... __proto__ - Object last __proto__ - 24
  • 25. Javascript the different good parts: Working with Objects and Prototypes 1 var car = { color: quot;whitequot;, speed: 0, accel: function(kmh) { this.speed += kmh; } }; // Make a supercar from car var supercar = { color: quot;blackquot;, __proto__: car }; // or also by cloning var supercar = Object.create(car, { color: quot;blackquot; }); 25
  • 26. Javascript the different good parts: Working with Objects and Prototypes 2 // Prototype relationship is dynamic car.wheels = 4; supercar.wheels; // 4 // Setting properties is local supercar.driver = quot;M. Knightquot;; car.driver; // undefined // Where properties are from? quot;driverquot; in supercar; // true quot;wheelsquot; in supercar; // true supercar.hasOwnProperty(quot;driverquot;); // true supercar.hasOwnProperty(quot;wheelsquot;); // false // Common to all objects supercar.toString(); // quot;[object Object]quot; car.isPrototypeOf(supercar); // true 26
  • 27. Javascript the different good parts: Objects Prototypal Inheritance 1  Prototypes are javascript way to share  Data  Behaviour 27
  • 28. Javascript the different good parts: Objects Prototypal Inheritance 2  Prototypal Inheritance  Vs Classical Inheritance  Simpler  No classes and objects, only objects  Easier  Work by examples, not abstractions  Powerful !!  Can simulate classical  Reverse not true  Shhhh, Don’t tell anyone  Easier to write spaghetti code 28
  • 29. Javascript the different good parts: Objects Prototypal Inheritance 3  Ok, I cheated  __proto__ available in mozilla only  Object.create coming in next revision of language  Javascript is schizophrenic  Prototypal nature  Wannabe classical 29
  • 30. Javascript the different good parts: Objects Constructor Functions 1  Constructor Functions  Boring  Function has a “prototype” property  The “prototype” property has a “constructor” property which points back to the Function object  Function can be invoked with the “new” operator  Create an object whose __proto__ is “prototype” property of Function  Initialize the object executing Function 30
  • 31. Javascript the different good parts: Objects Constructor Functions 2 // Constructor function // silently executes function Dog(name) { // Dog.prototype = { constructor: Dog } this.name = name; } Dog.prototype; // Object Dog.prototype.constructor; // return function Dog // Create a new Dog object // silently executes var fido = new Dog(quot;fidoquot;); // var fido = Object.create(Dog.prototype); // Dog.call(fido, quot;fidoquot;); fido.__proto__; // Object fido.__proto__.constructor; // Dog fido.constructor; // Dog (inherited by __proto__) fido.name // fido 31
  • 32. Javascript the different good parts: Objects Constructor Functions 3 function Rectangle(w, h) {  Why? this.w = w; Function is a constructor  this.h = h; } Function prototype is a class  Rectangle.prototype.higher = function() { this.h += 1 }; new is new  Feels classical, Feels familiar  var rect = new Rectangle(5,10);  Worst of both worlds  Unnecessarily complicated  Hide prototypal nature  Weird for classical programmers 32
  • 33. Javascript the different good parts: Objects Constructor Functions Fix  Fortunately there’s a Quick Fix // waiting for next javascript implementation... if (typeof Object.create !== 'function') { Object.create = function (o) { var F = function() {}; F.prototype = o; return new F(); }; } 33
  • 34. Javascript the different good parts: Arrays  No real arrays in javascript  They’re objects in disguise  special props and methods  Cool literal syntax 34
  • 35. Javascript the different good parts: Arrays Example 1 // array literal var numbers = [1, 2, 3, 4, 5]; // reference numbers[2]; // returns 3 numbers[10]; // returns undefined // length property numbers.length; // 5 // nice methods numbers.push(11); // now [1, 2, 3, 4, 5, 11] numbers.join(quot; quot;); // returns quot;1 2 3 4 5 11quot; 35
  • 36. Javascript the different good parts: Arrays Example 2 // actually... // indexes are just object properties numbers.2; // would return 3 // common object methods numbers.hasOwnProperty(0); // returns true // length = last numeric prop + 1 numbers[100] = 100; numbers.length; // returns 101 // ultimate proof typeof numbers; // returns quot;objectquot; 36
  • 37. Javascript the different good parts: Functional Programming  Iterators  Callbacks  Module Pattern  Curry  Memoization 37
  • 38. Javascript the different good parts: Functional Programming Iterators  Take control of loops  Reduce accidental complexity 38
  • 39. Javascript the different good parts: Iterators Example 1 // iterate on a collection function each(arr, func) { for (var i=0; i<arr.length; i++) { func(arr[i]); } } var ten = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; each(ten, function(i) { print i; }); // prints 12345678910 39
  • 40. Javascript the different good parts: Iterators Example 2 // maps a collection to a new one function map(arr, func) { var result = []; each(arr, function(i) { result.push(func(i)); }); return result; } var ten = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; map(ten, function(i) { return i * i; }); // returns [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 40
  • 41. Javascript the different good parts: Iterators Example 3 // filter elements of a collection function filter(arr, func) { var result = []; each(arr, function(i) { if (func(i)) { result.push(i); } }); return result; } var ten = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; filter(ten, function(i) { return i % 2 === 0; }); // returns [2, 4, 6, 8, 10] 41
  • 42. Javascript the different good parts: Iterators Example 4 // compute a single value from a collection function reduce(arr, func, start) { var result = start; each(arr, function(i) { result = func(i, result); }); return result; } var ten = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; reduce(ten, function(i, sum) { return i + sum; }); // returns 55 42
  • 43. Javascript the different good parts: Iterators Example 5 // Composability // square elements, then pick even ones, then sum reduce( filter( map(ten, function(i) { return i * i; } ), function(i) { return i % 2 === 0; } ), function(i, sum) { return i + sum; }, 0 ); 43
  • 44. Javascript the different good parts: Iterators Example 6 // Composability but easy to read var square = function(arr) { return map(arr, function(i) { return i * 2; }); } var even = function(arr) { return filter(arr, function(i) { return i % 2 === 0; }); } var sum = function(arr) { return reduce(arr, function(i, total) { return i + total; }, 0); } sum(even(square(ten))); 44
  • 45. Javascript the different good parts: Functional Programming Callbacks  Manage asynchronous communication  Hide complexity 45
  • 46. Javascript the different good parts: Callbacks Example 1 // Synchronous request var response = get(quot;http://www.google.comquot;); display(response); // Asynchronous with callback get(quot;http://www.google.comquot;, function(response) { display(response); }); 46
  • 47. Javascript the different good parts: Callbacks Example 2 // explicit complexity var response = get(quot;http://www.google.comquot;); if (response.completed) { if (response.httpCode === quot;200quot;) { display(response); } else { // http error } } else { // network error } 47
  • 48. Javascript the different good parts: Callbacks Example 3 // complexity hidden in the client code var response = get(quot;http://www.google.comquot;); if (success(response)) { display(response); } else { // error } // complexity hidden away get(quot;http://www.google.comquot;, { success: function(response) { display(response); } }); 48
  • 49. Javascript the different good parts: Functional Programming Module Pattern  Hide state and behaviour 49
  • 50. Javascript the different good parts: Module Pattern Example 1 var numbers = [quot;zeroquot;, quot;onequot;, quot;twoquot;, quot;threequot;, ...]; // GLOBAL BAD var numberToString = function(num) { return numbers[num]; } var numberToString = function(num) { // LOCAL SLOW var numbers = [quot;zeroquot;, quot;onequot;, quot;twoquot;, quot;threequot;, ...]; return numbers[num]; } var numberToString = function() { // PRIVATE AND FAST var numbers = [quot;zeroquot;, quot;onequot;, quot;twoquot;, quot;threequot;, ...]; return function(num) { return numbers[num]; } }(); 50
  • 51. Javascript the different good parts: Functional Programming Memoization  Cache computation  Speed up execution 51
  • 52. Javascript the different good parts: Memoization Example 1 // get pixels. maybe millions of them var pixels = getImagePixels(quot;image.jpgquot;); var getColor = function(pixel) { // ... computation on RGB values ... returns quot;blackquot;; // or quot;whitequot; or quot;greenquot; etc... } // find the color pixels.each(function(pixel) { var color = getColor(pixel); // store result }); 52
  • 53. Javascript the different good parts: Memoization Example 2 // wasted computation, cache it... var getColorCache = function(func) { var cache; // setup cache ... return function(pixel) { if (cache.missing(pixel)) { cache.store(pixel, func(pixel)); } return cache.get(pixel); } }(getColor); 53
  • 54. Javascript the bad parts  The features you should definitely avoid 54
  • 55. Javascript the bad parts 1  Global variables  Semicolon insertion // Good, returns { ok: true } // Very bad, returns undefined return { return ok: true { } ok: true }  Reserved words // Good // Very bad, error book[quot;classquot;]; book.class; var book = { var book = { quot;classquot;: quot;bookquot; class: quot;bookquot; } } 55
  • 56. Javascript the bad parts 2  Unicode  typeof // Not useful, returns quot;objectquot; typeof array; // Wrong, returns quot;objectquot; typeof null; // Inconsistent, returns quot;functionquot; or quot;objectquot; typeof /a/;  parseInt // Good, returns 8 // Wrong, returns 0 parseInt(quot;08quot;, 10); parseInt(quot;08quot;); 56
  • 57. Javascript the bad parts 3 +  Floating Point 0.2 + 0.1 === 0.3 // false  Phony Arrays arguments.join // returns undefined  Falsy values if (book.name == null) { ... // 2 errors, works by coincidence  == type coercion '' == '0' // false false == undefined // false 0 == '' // true false == null // false 0 == '0' // true null == undefined // true 57
  • 58. Javascript the bad parts 4  Objects are not hashes var cache; var word = getWord(); // returns quot;constructorquot; if (word in cache) { // ops, true  Deep for..in // safe way for (var i in list) { if (list.hasOwnProperty(i)) { // do something } }  Extending native prototypes kill kittens  Prototype library mess 58
  • 59. Credits  Thank to Douglas Crockford and his book  He knows the way 59