On Github DJCordhose / javascript-einfuehrung
Oliver Zeigermann / @DJCordhose
Online-Version: http://bit.ly/1VTfUZX
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<script>
alert("Hello World");
</script>
</head>
<body>
</body>
</html>
Run
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<div id="log"></div>
<script>
var element = document.getElementById("log");
element.innerHTML = "<h1>Hello World</h1>";
</script>
</body>
</html>
Run
node -e "console.log('Hello World');"
var map = {
feld1: 'Huhu',
zweites$Feld: "Auch sowas geht!"
};
console.log(typeof map === "object"); // true
console.log(map.feld1); // Huhu
console.log(map["zweites$Feld"]); // Auch sowas geht!
map.hund = "Ganz neu geht auch";
var string = "String";
typeof string === "string";
var int = 1;
typeof int === "number";
var float = 1.0;
typeof float === "number";
var bool = true;
typeof bool === "boolean";
var func = function() {};
typeof func === "function";
typeof michGibtEsNicht === "undefined";
var array = ["a", "b", "c"];
var el = array[2];
array[1] = 20;
typeof array === "object";
// fügt die 4 am Ende hinzu
array.push(4);
for (var i=0; i < array.length; i++) {
console.log(i + ": " + array[i]);
}
// Durch alle Feld-Namen iterieren
// Geht für Map / Object und für Array!
for (var i in map) {
console.log(i + ": " + map[i]);
}
function f2() {
console.log("Called!");
}
var result2 = f2();
result2 === undefined;
var f1 = function(p1, p2) {
return p1 + p2;
};
var result1 = f1(1,2);
result1 === 3;
function f1(p1) {
if (typeof p1 === 'undefined') {
return null;
} else {
return p1;
}
}
var result1 = f1(1);
console.log(result1 === 1);
var result2 = f1();
console.log(result2 === null);
function summe() {
var sum = 0;
for (var a in arguments) {
sum += arguments[a];
}
return sum;
}
var result5 = summe(1,2,3);
console.log(result5 === 6);
{
var huch = "Ich bin noch da";
}
console.log(huch); // Ich bin noch da
(function () {
var achso = "Ich bin weg";
}());
console.log(achso); // ReferenceError
var obj = {
field: 10,
log: function() {
console.log(this.field);
}
};
obj.log(); // 10
var field = "Reingelegt"; obj.log.call(this); // => ???
Der Prototyp kann nicht direkt, aber durch Aufruf von new gesetzt werden
/** @constructor */
function Person(name) {
this.name = name;
}
// Methode
Person.prototype.getName = function() {
return this.name;
};
var olli = new Person('Olli');
olli.getName() === 'Olli';
Ein Objekt ist instanceof aller seiner Prototypen
var olli = new Person('Olli');
Object.getPrototypeOf(olli) === Person.prototype;
olli instanceof Object;
olli instanceof Person;
function Person(name, gender) {
this.name = name;
this.gender = gender;
}
Person.prototype.getName = function() {
return this.name;
};
function Male(name) {
Person.call(this, name, "Male"); // super call
}
Male.prototype = Object.create(Person.prototype);
Male.prototype.getName = function() {
// super call
return "Mr " + Person.prototype.getName.call(this);
};
var olli = new Male('Olli');
olli.getName() === 'Mr Olli';
olli.gender === 'Male';
olli instanceof Male;
olli instanceof Person;
olli instanceof Object;
function Programmer(name, language) {
Person.call(this, name);
this.language = language;
}
_.extend(Programmer.prototype, Person.prototype);
// instead of
//Programmer.prototype = Object.create(Person.prototype);
Programmer.prototype.code = function() {
return this.getName() + " codes in " + this.language;
};
var programmer = new Programmer('Erna', 'JavaScript');
console.log(programmer.getName()); // Erna
console.log(programmer.code()); // Erna codes in JavaScript
console.log(programmer instanceof Programmer); // true
// true for Object.create, false for _.extend
console.log(programmer instanceof Person);
Module in JavaScript werden über Closures realisiert
Eine innere Funktion hat immer Zugriff auf alle Variablen und Parameter ihrer äußeren Funktion, auch wenn diese äußere Funktion bereits beendet ist.
Frei nach Douglas Crockford
function outer() {
var used = "Olli";
var unused = "Weg";
return (function() {
return "Text: " + used;
});
}
var inner = outer();
console.log(inner());
Eine Closure ist eine spezielle Art von Objekt, welche zwei Dinge kombiniert
Eine Funktion die Umgebung in welcher diese Funktion erzeugt wurde - diese Umgebung besteht aus allen lokalen Variablen und Parametern, die sichtbar waren als die Closure erzeugt wurdevar humanresources = (function () {
function InternalStuff() {
}
function Person(name) {
this.name = name;
// uses internal stuff
}
return {
Person: Person
};
})();
var olli = new humanresources.Person('Olli');
olli.name === 'Olli';
// TypeError: undefined is not a function
new humanresources.InternalStuff();
// in der Datei "eater.js"
var eatThis = function (name) {
console.log(name);
};
exports.eatThis = eatThis;
var eaterModule = require("eater");
eaterModule.eatThis(name);
// Ein Modul pro Datei:
// js/modules/accounting.js
define(function () {
return {
getIdNumberForName: function(name) {
// ...
}
};
});
require(['js/modules/accounting'], function (Accounting) {
var name = // ...
var id = Accounting.getIdNumberForName(name);
// ...
});
describe("Calculator", function () {
var data;
beforeEach(function () {
data = calculateMortgage(200000, 10, 7.5, 30);
});
afterEach(function() {
});
it("principle", function () {
expect(data.principle).toEqual(199990.00);
});
it("invalid", function () {
expect(function () {
calculateMortgage(1, 10, 7.5, 30);
}).toThrowError("You do not need any money");
});
});
it('that click calls calculate method', function() {
spyOn(formCtrl, 'calculateMortgage');
var btn = formCtrl.getCalculateButton();
btn.fireEvent('click');
expect(formCtrl.calculateMortgage).toHaveBeenCalled();
});
it('that fired event is received', function() {
spyOn(formCtrl, 'openMortgageEditor');
// main controller fires event that should be called by form controller
mainCtrl.fireEvent('edit', mortgage);
expect(formCtrl.openMortgageEditor).toHaveBeenCalledWith(mortgage);
});
<!DOCTYPE html>
<html>
<head>
<title>Hello World jQuery!</title>
<body>
<div id="log"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#log").html("<h1>Hello World</h1>");
});
</script>
</body>
</html>
Run
log.show();
var log = $("#log");
log.on('click', function(event) { // ... });
jQuery kann um eigene Funktionalität erweitert werden
$ als Factory
var log = $("#log");
Object.getPrototypeOf(log) === $.prototype; // true
log instanceof $; // true
Erweiterung der Funktionalität von $ über Plugins
$.fn === $.prototype;
$.fn.myPlugin = function() {
// ...
};
log.myPlugin();
Alle durch den Selektor gefundenen Elemente ausgeben
$.fn.dump = function() {
// iterate over all elements found by selector
this.each(function() {
// this is a DOM element,
// make it a $ object if desired
console.log($(this));
});
// return $ element for chaining
return this;
};
var log = $("#log");
log.dump().show(); // chaining does work
{
let a = 10;
// or
const a = 10;
a = 20;
// when a is const:
// => TypeError: Assignment to constant variable.
}
console.log(a);
// => ReferenceError: a is not defined
class Person {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
}
class Programmer extends Person {
constructor(name, language) {
super(name);
this.language = language;
}
code() {
return this.getName() + " codes in " + this.language;
}
}
const programmer = new Programmer('Erna', 'JavaScript');
console.log(programmer.code());
console.log(programmer instanceof Programmer); // true
console.log(programmer instanceof Person); // true