JavaScript
Java is to JavaScript as ham is to hamster
• The basic language
• The good parts
• The bad parts
• The salvation
The basic language
     Do what I mean
alert(”Hello world”);
function fib(n) {
    if (n == 1 || n == 2) {
        return 1;
    }
    return fib(n-1) + fib(n-2);
}
alert(fib(100));
var burt = new Person();
burt.age = 2;
burt.father = ”Gustav”;
try {
    var input = prompt();
    if (input <= 0) {
        throw ”Natural plz”;
    }
    alert(fib(input));
} catch (ex) {
    alert(”Doh: ” + ex);
}
var s1 = ”a ‘quoted’ string”;
var s2 = ’a ”quoted” string’;
var s3 = ”a ‘quoted” string”;
var a1 = [2, 3, 5, 7, 11];
var a2 = [”foo”, ”bar”];
var a3 = [2, ”foo”, null];
var p = /^[A-Z][a-z]*$/;
alert(”Jakob”.match(p));
alert(”iPhone”.match(p));
The good parts
First-class functions, object literals, closures, prototyping
                  and dynamic arguments
First-class functions
var burt = new Person();
burt.age = 2;
burt.father = ”Gustav”;
burt.tellAge = function() {
    alert(”Age: ” + this.age);
}
burt.tellAge();
burt.age = 3;
burt.tellAge();
var p = [2, 3, 5, 7, 11];
alert(p.filter(function(x) {
  return x & 1 == 0;
}));
get(”jsonip.com”, function(x) {
  alert(x);
});
Object literals
var burt = {
    age: 2,
    father: ”Gustav”,
    tellAge: function() {
      alert(”Age: ” + this.age);
    },
    employees: [”Jakob”, ... ]
}
var whichOne = prompt();
alert(burt[whichOne]);
for (var property in burt) {
    alert(property);
    alert(burt[property]);
}
burt.lookMom = ”a new value!”;
alert(Object.keys(burt));
alert(Object.values(burt));
var json = {
    ”xml”: ”no”,
    ”json”: true,
    ”numbers”: [1, 2, 3],
    ”functions”: false,
    ”recursive”: {
        ”guess”: ”Hell yeah!”
    }
}
Closures
class Counter {
    private int x = 0;
    public void count() {
        return x++;
    }
}
var c1 = new Counter();
var c2 = new Counter();
var a = [c1.count(),
         c1.count(),
         c2.count()];
alert(a);
function counter() {
    var x = 0;
    var f = function() {
        return x++;
    }
    return f;
}
var c1 = counter();
var c2 = counter();
var a = [c1(), c1(), c2()];
alert(a);
• C lets us forget stacks, heaps and jumps
• Java lets us forget memory management
• JavaScript lets us forget creating state
Prototyping
function Animal(name) {
    this.name = name;
    this.talk = function() {
        alert(this.name + ”: huh?”);
    }
}
var a = new Animal(”garfield”);
var b = new Animal(”lassie”);
alert(a.name);
alert(b.talk());
function Animal(name) {
    this.name = name;
}
Animal.prototype.talk = f() {
    alert(this.name + ”: huh?”);
}
var a = new Animal(”garfield”);
var b = new Animal(”lassie”);
alert(a.name);
alert(b.talk());
function clone(o) {
    function F() {}
    F.prototype = o;
    return new F();
}
var animal = { };
animal.talk = function() {
    alert(this.name + ": huh?");
};
var cat = clone(animal);
cat.talk = function() {
 alert(this.name + ": meow!");
}
var a = clone(cat);
a.name = "garfield";
a.talk();
The bad parts
  The bad and the ugly
IE6
The DOM
eval
var sum = eval(”1 + 4”);
eval(”var x = 10”);
eval(”alert(x + sum)”);
• Opens up the code for injection attacks
• Debugging becomes hell
• Slow execution
Tricky truthiness
if (x) {
    alert(”when?”);
}
• Not null
• Not undefined
• Not false
• Not 0
• Not the empty string
Missing ”var”
// Style #1
function foobar() {
    x = 10;
}
// Style #2
function foobar() {
    var x = 10;
}
// Style #3
var x;
function foobar() {
    x = 10;
}
fs.readFileSync(
  sourceDir + "/" + files[i],
  encoding = "utf8"
);
Silly semicolon
    insertion
function foobar() {
    var x = 1 + 2
    return x
}
function foobar() {
    var x = 1 + 2;
    return
    x;
}
with
with(burt) {
    alert(age);
    alert(father);
}
with(burt) {
    age = 3;
    foo = ”bar”;
}
parseInt
var a = parseInt(”123”);
var b = parseInt(”110”, 2);
var c = parseInt(”0123”);
Everything is an object
  (except when parsing numbers)
alert(x.toString());
alert(1.toString());
alert(1.0.toString());
alert(1..toString());
alert((1).toString());
The salvation
Good coding practices, common sense & static analysis

JavaScript @ CTK