Modern Javascript

Bas Bossink

Agenda

Disclaimer

About

Why javascript

Popularity

courtesy of http://langpop.corger.nl/

Ubiquitous

Performance

Performance

http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=all&lang=v8&lang2=yarv&data=u32
source: Computer Language Benchmarks Game

Performance

http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=all&lang=v8&lang2=gcc&data=u32
source: Computer Language Benchmarks Game

History

History

ECMAScript

courtesy of Luke Hoban
source: Modern Javascript, Luke Hoban

New in ECMAScript 5: Strict mode

(function() {
  'use strict';
  ...
}());
source: John Resig, ECMAScript 5 Strict Mode, JSON, and More

New in ECMAScript 5: Properties

var fred = {};
Object.defineProperty(fred, "name", {
        get: function() {
          console.log("Retrieving name: " + name);
          return name;
        },
        set: function(value) {
          console.log("Setting name to: " + value);
          name = value;
        }
    });

New in ECMAScript 5: JSON object

New in ECMAScript 5: Object

New in ECMAScript 5: Function bind

var Button = function(content) { this.content = content; };
Button.prototype.click = function() { 
  console.log(this.content + ' clicked'); 
}
var fred = new Button('OK');
var boundClick = fred.click.bind(fred);
boundClick();

New in ECMAScript 5: Array

JavaScript: The Good Parts

Object literals

var fred = {
  firstName: "Fred",
  lastName: "Flinstone",
  hometown: "Bedrock",
  wife: {
    firstName: "Wilma",
    lastName: "Flinstone"
  }
};

Functions

Closure

var myObject = (function () {
  var value = 0;

  return {
    increment: function (inc) {
        value += typeof inc === 'number' ? inc : 1;
    },
    getValue: function (  ) {
        return value;
    }
  };
}());
source: JavaScript: The Good Parts, Douglas Crockford

Inheritance

Prototypal Inheritance

var fred = {
  name : 'Fred Flinstone',
  saying : 'Yabadabadoo!',
  says : function() { return this.saying || ''; }
};
var barney = Object.create(fred);
barney.name = 'Barney Rubble';
barney.saying = 'Hi Fred';

Arrays

Awful Parts

Awful Parts

Falsy values:

Value Type
0 Number
NaN Number
'' String
false Boolean
null Object
undefined Undefined

Bad Parts

Bad Parts

Questions?