JavaScript im Jahr 2011
Tim „Scytale“ Weber <http://scytale.name/>
28. August 2011
2. Large Hackerspace Convention, shackspace Stuttgart
Feedback? Twitter @Scytale oder http://scytale.name/contact/
Tim „Scytale“ Weber <http://scytale.name/>
28. August 2011
2. Large Hackerspace Convention, shackspace Stuttgart
Feedback? Twitter @Scytale oder http://scytale.name/contact/
onclick
, onload
, …typeof
-Operator gibt einen String mit dem Typ zurücktypeof 3 // "number" (-3.141).toString() // "-3.141" 5e3 // 5000
\n
, \u0041
etc.)typeof "foo" // "string" typeof typeof 3 // "string" "A" === '\u0041' // true 'mööp♥'.length // 5
typeof null // "object" null.toString() // (Exception)
true
oder false
typeof true // "boolean" true.toString() // "true"
typeof
, obwohl Konstante undefined
existiertundefined // undefined typeof undefined // "undefined" typeof x // "undefined" typeof x === 'undefined' // true x == undefined // (Exception: „x is not defined“)
var x = { foo: "bar", // entspricht "foo": "bar" "true": "Spandau Ballet", // bei reservierten oder nicht-Wörtern 3.141: "Pi" // Zahl als Key }; typeof x // "object" x["foo"] === x.foo // true x.true // "Spandau Ballet" x["3.141"] // "Pi"
var x = [2, 4, 6]; x[1] // 4 x[5] // undefined x.length // 3 x[5] = 9000 x.length // 6 (!!) x[4] // undefined
throw { type: "BullshitException", message: "I’m in space!" }; throw "PENIS!"; throw 5; throw true;
var sum = function (a, b) { // ähnlich: function sum(a, b) { return a + b; }; sum(3, 8) // 11 sum("PEN", 15) // "PEN15" sum.foo = "bar";
var
definierte Variablen: lokalvar
definierte: im window
-Objekt („global“)x = 3; // global var y = 4; // auch global weil im äußersten Scope definiert var foo = function (a) { var z = 9; y += a; return a * 2; }; foo(15) // 30 x // 3 y // 19 z // (Exception weil undefiniert)
Objektorientierung
var animal = { lives: 1, "die": function () { this.lives--; } }; var cat = { lives: 9 }; cat.prototype = animal;
this
var o = { val: 3, foo: function () { return this.val * 2; } }; o.foo() // 6 var o2 = { foo: o.foo }; o2.foo() // NaN (weil this auf o2 zeigt) var blub = o.foo; blub() // NaN
var animal = function (startLives) { var lives = startLives; return { "die": function () { lives--; } } }; var bello = animal(1); bello.lives // undefined bello.die();
microjs.com
<body onload="alert('foo!');">
<form onsubmit="alert('bumm');">
jQuery(function () { alert('foo!'); });
$("form").submit(function () { foo(); });
Schlecht:
<a href="javascript:loadTab('info');">
Gut:
<a id="infolink" href="/info"> <script> $("#infolink").click(function () { loadTab('info'); return false; }); </script>
<html class="no-js">
wird zu
<html class=" js no-touch no-history multiplebgs boxshadow opacity no-cssanimations csscolumns cssgradients csstransforms no-csstransitions fontface localstorage sessionstorage svg no-inlinesvg tk-loaded wf-active js flexbox canvas …">
if (typeof di === 'undefined') { di = {}; } di.JS2011Talk = { run: function () { … } };
==
0 == '' // true 0 == '0' // true '' == '0' // false
0 == true // false !'0' // false !!'0' // true
new
und this
möglichst vermeiden==
vermeiden, stattdessen ===
if
s"use strict"
i ++ j