Skip to content

Commit 1d00c44

Browse files
Updated source code of chapter 2 for second edition
1 parent 7b41b7a commit 1d00c44

File tree

104 files changed

+1180
-1100
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1180
-1100
lines changed

Kapitel2/Listing_2-1.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
function addition(x, y) {
2-
return x + y;
3-
}
1+
function add(x, y) {
2+
return x + y;
3+
}

Kapitel2/Listing_2-10.js

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
function operationenContainer(x, y) {
2-
var addition = function(x, y) {
3-
return x + y;
4-
}
5-
var subtraktion = function(x, y) {
6-
return x - y;
7-
}
8-
var multiplikation = function(x, y) {
9-
return x * y;
10-
}
11-
var division = function(x, y) {
12-
return x / y;
13-
}
14-
console.log(addition(x, y));
15-
console.log(subtraktion(x, y));
16-
console.log(multiplikation(x, y));
17-
console.log(division(x, y));
1+
function operationFactory(name) {
2+
switch (name) {
3+
case 'add':
4+
return (x, y) => x + y;
5+
case 'subtract':
6+
return (x, y) => x - y;
7+
case 'multiply':
8+
return (x, y) => x * y;
9+
case 'divide':
10+
return (x, y) => x / y;
11+
default:
12+
return () => NaN;
13+
}
1814
}
19-
operationenContainer(2,2);

Kapitel2/Listing_2-100.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function calculate() {
2+
console.log('calculate()');
3+
return 'Ergebnis';
4+
}
5+
function getResult() {
6+
const result = calculate(); // Hier die einmalige Berechnung
7+
getResult = function() {
8+
return result; // ab zweitem Aufruf
9+
};
10+
return result; // erster Aufruf
11+
}
12+
console.log(getResult()); // Ausgabe: "calculate()", dann "Ergebnis"
13+
console.log(getResult()); // Ausgabe: "Ergebnis"
14+
console.log(getResult()); // Ausgabe: "Ergebnis"
15+
console.log(getResult()); // Ausgabe: "Ergebnis"

Kapitel2/Listing_2-101.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const array = ['1', 'Max', '2', '3', '4', '5', 'IoT', '6', '7', '8', '9'];
2+
const result = array
3+
.map(x => parseInt(x))
4+
.filter(x => !isNaN(x))
5+
.reduce((x, y) => x + y);
6+
console.log(result);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const Rx = require('rxjs');
2+
const array = ['1', 'Max', '2', '3', '4', '5', 'IoT', '6', '7', '8', '9'];
3+
const stream = Rx.Observable.from(array);
4+
stream
5+
.map(x => parseInt(x))
6+
.filter(x => !isNaN(x))
7+
.reduce((x, y) => x + y)
8+
.subscribe(
9+
x => console.log(x),
10+
error => console.error(error),
11+
() => console.log('Fertig')
12+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function init() {
2+
const dragTarget = document.getElementById('drag-target');
3+
// Datenstrom für das mouseup-Event
4+
const mouseUpStream = Rx.Observable.fromEvent(document, 'mouseup');
5+
// Datentrom für das mousemove-Event
6+
const mouseMoveStream = Rx.Observable.fromEvent(document, 'mousemove');
7+
// Datenstrom für das mousedown-Event
8+
const mouseDownStream = Rx.Observable.fromEvent(dragTarget, 'mousedown');
9+
mouseDownStream
10+
.mergeMap(mouseDownEvent =>
11+
mouseMoveStream
12+
.map(mouseMoveEvent => {
13+
mouseMoveEvent.preventDefault();
14+
return {
15+
left: mouseMoveEvent.clientX - mouseDownEvent.offsetX,
16+
top: mouseMoveEvent.clientY - mouseDownEvent.offsetY
17+
};
18+
})
19+
.takeUntil(mouseUpStream)
20+
)
21+
.subscribe(position => {
22+
console.log(
23+
'Relative Position:',
24+
'links:',
25+
position.left,
26+
'oben:',
27+
position.top
28+
);
29+
});
30+
}
31+
document.addEventListener('DOMContentLoaded', init);

Kapitel2/Listing_2-11.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
var operationen = {
2-
addition: function(x, y) {
3-
return x + y;
4-
},
5-
subtraktion: function(x, y) {
6-
return x - y;
7-
},
8-
multiplikation: function(x, y) {
9-
return x * y;
10-
},
11-
division: function(x, y) {
12-
return x / y;
13-
}
1+
function operationsContainer(x, y) {
2+
const add = function(x, y) {
3+
return x + y;
4+
};
5+
const subtract = function(x, y) {
6+
return x - y;
7+
};
8+
const multiply = function(x, y) {
9+
return x * y;
10+
};
11+
const divide = function(x, y) {
12+
return x / y;
13+
};
14+
console.log(add(x, y));
15+
console.log(subtract(x, y));
16+
console.log(multiply(x, y));
17+
console.log(divide(x, y));
1418
}
15-
console.log(operationen.addition(2,2));
16-
console.log(operationen.subtraktion(2,2));
17-
console.log(operationen.multiplikation(2,2));
18-
console.log(operationen.division(2,2));
19+
operationsContainer(2, 2);

Kapitel2/Listing_2-12.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
/* Funktioniert nur in ES6 */
2-
var operationen = {
3-
addition(x, y) {
4-
return x + y;
5-
},
6-
subtraktion(x, y) {
7-
return x - y;
8-
},
9-
multiplikation(x, y) {
10-
return x * y;
11-
},
12-
division(x, y) {
13-
return x / y;
14-
}
15-
}
1+
const operations = {
2+
add: function(x, y) {
3+
return x + y;
4+
},
5+
subtract: function(x, y) {
6+
return x - y;
7+
},
8+
multiply: function(x, y) {
9+
return x * y;
10+
},
11+
divide: function(x, y) {
12+
return x / y;
13+
}
14+
};
15+
console.log(operations.add(2, 2));
16+
console.log(operations.subtract(2, 2));
17+
console.log(operations.multiply(2, 2));
18+
console.log(operations.divide(2, 2));

Kapitel2/Listing_2-13.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
var person = {
2-
name: 'Max', // Objekteigenschaft
3-
getName: function() {
4-
return this.name;
5-
}
6-
}
7-
console.log(person.getName()); // Ausgabe: Max
1+
const operations = {
2+
add(x, y) {
3+
return x + y;
4+
},
5+
subtract(x, y) {
6+
return x - y;
7+
},
8+
multiply(x, y) {
9+
return x * y;
10+
},
11+
divide(x, y) {
12+
return x / y;
13+
}
14+
};

Kapitel2/Listing_2-14.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
function getNameGlobal() {
2-
return this.name;
3-
}
4-
console.log(getNameGlobal()); // undefined
1+
const person = {
2+
firstName: 'Max', // Objekteigenschaft
3+
getFirstName: function() {
4+
return this.firstName;
5+
}
6+
};
7+
console.log(person.getFirstName()); // Ausgabe: Max

Kapitel2/Listing_2-15.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var name = "globaler Name";
2-
function getNameGlobal() {
3-
return this.name;
1+
function getFirstNameGlobal() {
2+
return this.name;
43
}
5-
console.log(getNameGlobal()); // Ausgabe: globaler Name
4+
console.log(getFirstNameGlobal()); // undefined

Kapitel2/Listing_2-16.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
"use strict";
2-
var name = "globaler Name";
3-
function getNameGlobal() {
4-
return this.name;
1+
firstName = 'globaler Name';
2+
function getFirstNameGlobal() {
3+
return this.firstName;
54
}
6-
// console.log(getNameGlobal()); // Fehler: this ist nicht definiert
5+
console.log(getFirstNameGlobal()); // Ausgabe: globaler Name

Kapitel2/Listing_2-17.js

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
function getNameGlobal() {
2-
return this.name;
1+
'use strict';
2+
const firstName = 'globaler Name';
3+
function getFirstNameGlobal() {
4+
return this.firstName;
35
}
4-
var person2 = {
5-
name : 'Moritz',
6-
getName : getNameGlobal
7-
}
8-
var wuestenrockKoenige = {
9-
name : 'Kyuss',
10-
getName : getNameGlobal
11-
}
12-
console.log(person2.getName()); // Ausgabe: Moritz
13-
console.log(wuestenrockKoenige.getName()); // Ausgabe: Kyuss
6+
console.log(getFirstNameGlobal()); // Fehler: this ist nicht definiert

Kapitel2/Listing_2-18.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
function beispiel(x) {
2-
if(x) {
3-
var y = 4711;
4-
}
5-
for(var i=0; i<4711; i++) {
6-
/* Irgendwas machen */
7-
}
8-
console.log(y);
9-
console.log(i);
10-
}
11-
beispiel(true);
1+
const anotherPerson = {
2+
firstName: 'Moritz',
3+
getFirstName: getFirstNameGlobal
4+
};
5+
const yetAnotherPerson = {
6+
firstName: 'Peter',
7+
getFirstName: getFirstNameGlobal
8+
};
9+
console.log(anotherPerson.getFirstName()); // Ausgabe: Moritz
10+
console.log(yetAnotherPerson.getFirstName()); // Ausgabe: Peter

Kapitel2/Listing_2-19.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
function beispiel(x) {
2-
console.log(y);
3-
console.log(i);
4-
if(x) {
5-
var y = 4711;
6-
}
7-
for(var i=0; i<4711; i++) {
8-
/* Irgendwas machen */
9-
}
1+
function example(x) {
2+
if (x) {
3+
var y = 4711;
4+
}
5+
for (var i = 0; i < 4711; i++) {
6+
/* Irgendwas machen */
7+
}
8+
console.log(y);
9+
console.log(i);
1010
}
11-
beispiel(true); // Ausgabe: undefined
11+
example(true);

Kapitel2/Listing_2-2.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function addition(x, y) {
2-
return x + y;
1+
function add(x, y) {
2+
return x + y;
33
}
4-
var operation = addition;
4+
const operation = add;

Kapitel2/Listing_2-20.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
function beispiel(x) {
2-
var y;
3-
var i;
4-
console.log(y);
5-
console.log(i);
6-
if(x) {
7-
y = 4711;
8-
}
9-
for(i=0; i<4711; i++) {
10-
}
1+
function example(x) {
2+
console.log(y);
3+
console.log(i);
4+
if (x) {
5+
var y = 4711;
6+
}
7+
for (var i = 0; i < 4711; i++) {
8+
/* Irgendwas machen */
9+
}
1110
}
12-
beispiel(true);
11+
example(true); // Ausgabe: undefined

Kapitel2/Listing_2-21.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
function beispiel(x) {
2-
var y, i;
3-
console.log(y);
4-
console.log(i);
5-
if(x) {
6-
y = 4711;
7-
}
8-
for(i=0; i<4711; i++) {
9-
}
1+
function example(x) {
2+
var y;
3+
var i;
4+
console.log(y);
5+
console.log(i);
6+
if (x) {
7+
y = 4711;
8+
}
9+
for (i = 0; i < 4711; i++) {}
1010
}
11-
beispiel(true);
11+
example(true);

Kapitel2/Listing_2-22.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function example(x) {
2+
var y, i;
3+
console.log(y);
4+
console.log(i);
5+
if (x) {
6+
y = 4711;
7+
}
8+
for (i = 0; i < 4711; i++) {}
9+
}
10+
example(true);

Kapitel2/Listing_2-23.js

-13
This file was deleted.

0 commit comments

Comments
 (0)