<!doctype html>
|
<html>
|
<head>
|
<meta charset="utf-8">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
|
<title>reveal.js</title>
|
|
<link rel="stylesheet" href="dist/reset.css">
|
<link rel="stylesheet" href="dist/reveal.css">
|
<link rel="stylesheet" href="dist/theme/night-custom.css">
|
|
<!-- Theme used for syntax highlighted code -->
|
<link rel="stylesheet" href="./node_modules/highlight.js/styles/atom-one-dark-reasonable.css">
|
</head>
|
<body>
|
<div class="reveal">
|
<div class="slides">
|
<section>
|
<h2>Web-Entwicklung</h2>
|
</section>
|
<section>
|
<section>
|
<h2>Agenda</h2>
|
</section>
|
<section>
|
<h3>Grundlagen</h3>
|
<ul>
|
<li>HTML/CSS/JS</li>
|
<li>HTTP-Protokoll</li>
|
<li>JSON</li>
|
</ul>
|
</section>
|
<section>
|
<h3>Fortgeschrittenes</h3>
|
<ul>
|
<li>JavaScript</li>
|
<li>Responsive Design</li>
|
<li>NodeJS</li>
|
<li>WebComponents / lit</li>
|
<li>Datenbank</li>
|
</ul>
|
</section>
|
<section>
|
<h3>Architektur & Paradigmen</h3>
|
<ul>
|
<li>Funktionale Programmierung</li>
|
<li>Objektorientierte Programmierung</li>
|
<li>Entwurfsmuster (Singleton, Factory, Builder...)</li>
|
<li>SOLID-Prinzipien</li>
|
<li>Dependency Injection</li>
|
<li>MVC</li>
|
<li>Redux</li>
|
</ul>
|
</section>
|
<section>
|
<h3>Interessante Technologien</h3>
|
<ul>
|
<li>Canvas-Element</li>
|
<li>CSS-Animationen</li>
|
<li>WebSockets</li>
|
<li>WebWorker</li>
|
<li>ServiceWorker</li>
|
<li>WebRTC</li>
|
<li>WebGL</li>
|
<li>Progressive Web Application</li>
|
<li>CEF / Electron</li>
|
<li>WebAssembly</li>
|
</ul>
|
</section>
|
<section>
|
<h3>Betrieb</h3>
|
<ul>
|
<li>Server einrichten</li>
|
<li>SSL- / TLS-Zertifikate</li>
|
</ul>
|
</section>
|
<section>
|
<h3>Workflows</h3>
|
<ul>
|
<li>Git</li>
|
<li>Test Driven Development</li>
|
<li>Continuous Integration / Continuous Delivery</li>
|
</ul>
|
</section>
|
</section>
|
<section>
|
<section>
|
<h2>HTML</h2>
|
</section>
|
<section>
|
<ul>
|
<li>HyperText Markup Language</li>
|
<li>Erste Version 1993 von Tim Berners-Lee</li>
|
<li>Dokumentationsmedium</li>
|
<li>Ursprünglich rein akademische Verwendung</li>
|
<li>Verlinkungen der Dokumente macht sie "hyper"</li>
|
</ul>
|
</section>
|
<section>
|
<table>
|
<tr><th>Version</th><th>Erscheinung</th><th>Elemente ca.</th></tr>
|
<tr><td>HTML 1.0</td><td>1993</td><td>20</td></tr>
|
<tr><td>HTML 2.0</td><td>1995</td><td>50</td></tr>
|
<tr><td>HTML 3.2</td><td>1997</td><td>70</td></tr>
|
<tr><td>HTML 4.01</td><td>1999</td><td>90</td></tr>
|
<tr><td>HTML 5.0</td><td>2014</td><td>110</td></tr>
|
</table>
|
</section>
|
<section>
|
<ul>
|
<li>Heute ein "lebender Standard" (Living Standard)</li>
|
<li>Stetige inkrementelle Erweiterung</li>
|
</ul>
|
</section>
|
<section>
|
<h3>Grundgerüst</h3>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<script type="text/template">
|
<!DOCTYPE html>
|
<html lang="de">
|
<head>
|
<meta charset="UTF-8">
|
</head>
|
<body>
|
</body>
|
</html>
|
</script>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Beispiel</h3>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<script type="text/template">
|
<!DOCTYPE html>
|
<html lang="en">
|
<head>
|
<meta charset="UTF-8">
|
<title>Title</title>
|
</head>
|
<body>
|
<h1>Lorem Ipsum</h1>
|
<div>
|
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing</p>
|
<a href="/assets/html/simple2.html">Weitere Infos</a>
|
</div>
|
</body>
|
</html>
|
</script>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Beispiel</h3>
|
<iframe data-src="/assets/html/simple.html"></iframe>
|
</section>
|
<section>
|
<h3>Aufbau eines Elements</h3>
|
<pre>
|
<code class="html" data-trim>
|
<script type="text/template">
|
<span id="my-id" class="super important">Inhalt</span>
|
</script>
|
</code>
|
</pre>
|
<pre>
|
<code class="html" data-trim>
|
<script type="text/template">
|
<!-- HTML 4.01 -->
|
<input id="input" class="super important" disabled="disabled" />
|
</script>
|
</code>
|
</pre>
|
<pre>
|
<code class="html" data-trim>
|
<script type="text/template">
|
<!-- HTML 5 -->
|
<input id="input2" class="super important" disabled>
|
</script>
|
</code>
|
</pre>
|
</section>
|
</section>
|
<section>
|
<section>
|
<h2>CSS</h2>
|
</section>
|
<section>
|
<ul>
|
<li>Cascading Style Sheet</li>
|
<li>Nach größerer Verbreitung von HTML</li>
|
<li>Webseiten ansprechend gestalten</li>
|
</ul>
|
</section>
|
<section>
|
<img data-src="/assets/images/css-sucks.png">
|
</section>
|
<section>
|
<h3>Beispiel</h3>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
selector [, selector, selector, ...] {
|
property: value;
|
}
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Beispiel</h3>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
html {
|
background-color: red;
|
font-size: 30px;
|
}
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Beispiel</h3>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<script type="text/template">
|
<html lang="en">
|
<head>
|
...
|
<style>
|
html {
|
background-color: red;
|
font-size: 30px;
|
}
|
</style>
|
</head>
|
<body>
|
...
|
</body>
|
</html>
|
</script>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Beispielhafte CSS-Eigenschaften</h3>
|
<ul>
|
<li>background-color: red</li>
|
<li>font-family: Georgia, serif, Arial</li>
|
<li>text-decoration: line-through underline</li>
|
</ul>
|
</section>
|
<section>
|
<h3>Selektoren</h3>
|
<ul>
|
<li>Umfangreiche Element-Selektoren zur Anwendung der Styles</li>
|
<li>Kinder, Enkel...</li>
|
<li>Geschwister, direkte Nachfolger</li>
|
<li>Attribute</li>
|
<li>Pseudoklassen</li>
|
</ul>
|
</section>
|
<section>
|
<h3>Selektoren</h3>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
#my-id /* ID */
|
html, body /* Mehrfachselektion */
|
div > p /* p die direkt unterhalb eines div sind */
|
div p /* p die irgendwo unterhalb eines div sind */
|
a:visited /* besuchte Links */
|
div:hover /* divs über die man mit der Maus fährt */
|
span.important /* span mit der Klasse "important" */
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Layout</h3>
|
<p>Es gibt zwei grundlegende Blocktypen:</p>
|
<p><code>block</code></p>
|
<p><code>inline</code></p>
|
</section>
|
<section>
|
<h3>Block</h3>
|
<p>Beansprucht eine ganze Zeile und verursacht Zeilenumbrüche</p>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
display: block;
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Kann Breite und Höhe haben</p>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
display: block;
|
width: 20px;
|
height: 20px;
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Inline</h3>
|
<p>Wie einfacher Text, der mit anderen in der gleichen Zeile stehen kann</p>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
display: inline;
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Festlegen von Breite und Höhe sind wirkungslos</p>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
display: inline;
|
width: 20px; /* kein Effekt */
|
height: 20px; /* kein Effekt */
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Inline-Block</h3>
|
<p>Mischform von <code>inline</code> und <code>block</code></p>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
display: inline-block;
|
width: 20px;
|
height: 20px;
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Spezifität</h3>
|
<p>Regeln können andere Regeln überschreiben</p>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<script type="text/template">
|
<div style="display: inline-block"></div>
|
</script>
|
</code>
|
</pre>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
#id:hover { display: none }
|
|
#id { display: block }
|
|
.class.clazz { display: inline }
|
|
div { display: inline-block }
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Box Model</h3>
|
<p>Es gibt zwei Varianten des Modells:</p>
|
<p><code>content-box</code> (default)</p>
|
<p>border-box</p>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
box-sizing: content-box;
|
box-sizing: border-box;
|
</code>
|
</pre>
|
</section>
|
<section>
|
<img data-src="/assets/images/css-box-model.png">
|
</section>
|
<section>
|
<h3>Beispiel</h3>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
div {
|
border: 3px solid red;
|
padding: 10px;
|
width: 100px;
|
}
|
|
.border-box {
|
box-sizing: border-box;
|
}
|
|
.content-box {
|
box-sizing: content-box;
|
}
|
</code>
|
</pre>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<script type="text/template">
|
<div class="content-box">content-box</div>
|
<div class="border-box">border-box</div>
|
</script>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Beispiel</h3>
|
<iframe data-src="/assets/html/box-sizing.html"></iframe>
|
</section>
|
<section>
|
<h3>Positioning</h3>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
/* default, da wo es in der Seite steht */
|
position: static;
|
/* relativ zu seiner eigentlichen Position */
|
position: relative;
|
/* relativ zu seinem nächsten non-static parent o. window */
|
position: absolute;
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
position: relative;
|
position: absolute;
|
</code>
|
</pre>
|
<p>erlauben Verschiebung mittels</p>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
top: 10px;
|
left: 10px;
|
right: 10px;
|
bottom: 10px;
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Beispiel Menü</h3>
|
<iframe data-src="/assets/html/css-menu.html"></iframe>
|
</section>
|
<section>
|
<pre>
|
<code class="css" data-trim data-line-numbers>
|
.menu-item {
|
display: inline-block;
|
|
position: relative;
|
}
|
|
.menu-item .children {
|
display: none;
|
|
position: absolute;
|
}
|
|
.menu-item:hover .children {
|
display: block;
|
}
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<script type="text/template">
|
<div class="menu">
|
<div class="menu-item">Eins</div>
|
<div class="menu-item">
|
Hover Me!
|
<div class="children">
|
<div>Item 1</div>
|
<div>Item 2</div>
|
</div>
|
</div>
|
<div class="menu-item">Drei</div>
|
</div>
|
</script>
|
</code>
|
</pre>
|
</section>
|
</section>
|
<section>
|
<section>
|
<h2>JS</h2>
|
</section>
|
<section>
|
<ul>
|
<li>JavaScript stammt aus dem Jahr 1995 von Netscape</li>
|
<li>Syntaktisch an Java angelehnt</li>
|
<li>Ursprünglich nur optionale, verzichtbare Effekte (unobstrusive)</li>
|
<li>Mittlerweile von ECMA standardisiert</li>
|
</ul>
|
</section>
|
<section>
|
<table>
|
<tr><th>Version</th><th>ECMA Standard</th><th>Jahr</th></tr>
|
<tr><td>ES1</td><td>ECMAScript 1</td><td>1997</td></tr>
|
<tr><td>ES2</td><td>ECMAScript 2</td><td>1998</td></tr>
|
<tr><td><strong>ES3</strong></td><td><strong>ECMAScript 3</strong></td><td><strong>1999</strong></td></tr>
|
<tr><td><strong>ES5</strong></td><td><strong>ECMAScript 5</strong></td><td><strong>2009</strong></td></tr>
|
<tr><td>ES6</td><td>ECMAScript 2015</td><td>2015</td></tr>
|
<tr><td></td><td>...</td><td></td></tr>
|
<tr><td></td><td>ECMAScript 2020</td><td>2020</td></tr>
|
</table>
|
</section>
|
<section>
|
<p>Code wird zwischen <code>script</code>-Tags geschrieben</p>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<!DOCTYPE html>
|
<html lang="de">
|
<head>
|
<meta charset="UTF-8">
|
</head>
|
<body>
|
<script>// code</script>
|
</body>
|
</html>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>DOM-API</p>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
// findet das erste Element
|
const parent = document.querySelector("#id");
|
|
// findet alle Elemente
|
const elements = document.querySelectorAll("#id");
|
|
// Element erstellen
|
const newElement = document.createElement("div");
|
|
newElement.addEventListener("click", (event) => {
|
// Click-Event
|
});
|
|
// Element anhängen
|
parent.appendChild(newElement);
|
|
newElement.remove();
|
</code>
|
</pre>
|
</section>
|
</section>
|
<section>
|
<section>
|
<h2>Auslagern in verschiedene Dateien</h2>
|
</section>
|
<section>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<!DOCTYPE html>
|
<html>
|
<head>
|
<!-- CSS -->
|
<link href="styles.css" rel="stylesheet">
|
</head>
|
<body>
|
<!-- JS -->
|
<script src="index.js"></script>
|
</body>
|
</html>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Parsen eines HTML-Dokuments</p>
|
<pre>
|
<code class="html" data-trim data-line-numbers="1|2|3|4|5|6|7|8|9|10">
|
<script type="text/template">
|
<!DOCTYPE html>
|
<html lang="de">
|
<head>
|
<meta charset="UTF-8">
|
<title>Coole Seite 😀</title>
|
</head>
|
<body>
|
<div>Ganz langer Text</div>
|
</body>
|
</html>
|
</script>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Verschiedene Varianten, JavaScript erst am Ende auszuführen</p>
|
<p>Damals mit jQuery:</p>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<!DOCTYPE html>
|
<html>
|
<head>
|
<script>
|
$(document).ready((event) => {
|
const element = document.querySelector("#hello");
|
});
|
</script>
|
</head>
|
<body>
|
<div id="hello">Hello World!</div>
|
</body>
|
</html>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p><code>script</code>-Tag am Ende</p>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<!DOCTYPE html>
|
<html>
|
<head>
|
</head>
|
<body>
|
<div>Hello World!</div>
|
...
|
<script src="index.js"></script>
|
</body>
|
</html>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Event-basiert, z.B. per DOMContentLoaded</p>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<!DOCTYPE html>
|
<html>
|
<head>
|
<script>
|
document.addEventListener("DOMContentLoaded", (event) => {
|
// wird ausgeführt, sobald das Dokument geparst wurde
|
const element = document.querySelector("#hello");
|
});
|
</script>
|
</head>
|
<body>
|
<div id="hello">Hello World!</div>
|
</body>
|
</html>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Moderne Variante</p>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<script src="index.js" defer></script>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h2><code>document</code>-Operationen</h2>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
document.open();
|
document.write("<div>Booo!</div>");
|
document.close();
|
</code>
|
</pre>
|
</section>
|
<section>
|
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/write">
|
<img data-src="/assets/images/document.write.jpg">
|
</a>
|
</section>
|
<section>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<body>
|
<script>document.write("<p>Dies ist ein Text.</p>")</script>
|
<button>Dunkle Magie</button>
|
<script>
|
const button = document.querySelector("button");
|
|
button.addEventListener("click", () => {
|
document.write("Und pfutsch!");
|
});
|
</script>
|
</body>
|
</code>
|
</pre>
|
</section>
|
</section>
|
<section>
|
<section>
|
<h2>HTTP-Protokoll</h2>
|
</section>
|
<section>
|
<img data-src="/assets/images/osi-model.jpg">
|
</section>
|
<section>
|
<ul>
|
<li>OSI-Schichten 5, 6 und 7</li>
|
<li>Anfrage-Antwort-Prinzip</li>
|
<li>Basiert auf einfachem Text</li>
|
</ul>
|
</section>
|
<section>
|
<table>
|
<tr><th>Version</th><th>Jahr</th></tr>
|
<tr><th>0.9</th><th>1991</th></tr>
|
<tr><th>1.0</th><th>1996</th></tr>
|
<tr><th>1.1</th><th>1999</th></tr>
|
<tr><th>2.0</th><th>2015</th></tr>
|
<tr><th>3.0</th><th>2022</th></tr>
|
</table>
|
</section>
|
<section>
|
<p>Request an www.google.de</p>
|
<pre>
|
<code class="http" data-trim data-line-numbers>
|
GET / HTTP/1.1
|
Host: www.google.de
|
User-Agent: curl/7.88.1
|
Accept: */*
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Response von www.google.de</p>
|
<pre>
|
<code class="http" data-trim data-line-numbers>
|
HTTP/1.1 200 OK
|
Date: Tue, 04 Apr 2023 15:45:26 GMT
|
Expires: -1
|
Cache-Control: private, max-age=0
|
Content-Type: text/html; charset=ISO-8859-1
|
Content-Security-Policy-Report-Only: [...]
|
Server: gws
|
X-XSS-Protection: 0
|
X-Frame-Options: SAMEORIGIN
|
Set-Cookie: [...]
|
Accept-Ranges: none
|
Vary: Accept-Encoding
|
Transfer-Encoding: chunked
|
|
<!doctype html><html>...</html>
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Einfach mal testen:</p>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
curl -v --http1.0 http://www.google.de
|
curl -v --http1.1 http://www.google.de
|
curl -v --http2 http://www.google.de
|
curl -v --http2-prior-knowledge http://www.google.de
|
curl -v --http3 http://www.google.de
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Verschiedene "Verben" bzw. "Methoden"</p>
|
<ul>
|
<li>GET</li>
|
<li>POST</li>
|
<li>PUT</li>
|
<li>PATCH</li>
|
<li>DELETE</li>
|
<li>...</li>
|
</ul>
|
</section>
|
<section>
|
<p>Beispiel:</p>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
git clone https://git.furnco.de/r/public/web-development/examples.git
|
cd examples/templates/005-http-rest
|
node index.js
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="text" data-trim data-line-numbers>
|
curl -X GET -is http://localhost:3000/todo
|
curl -X POST -H "Content-Type: application/json" -d '{"name": "Einkaufen"}' -is http://localhost:3000/todo
|
curl -X POST -H "Content-Type: application/json" -d '{"name": "Putzen"}' -is http://localhost:3000/todo
|
curl -X PUT -H "Content-Type: application/json" -d '{"name": "Schlafen"}' -is http://localhost:3000/todo/1
|
curl -X DELETE -is http://localhost:3000/todo/0
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="text" data-trim data-line-numbers>
|
HTTP/1.1 200 OK
|
X-Powered-By: Express
|
Content-Type: application/json; charset=utf-8
|
Content-Length: 13
|
ETag: W/"d-KMmUcQ1kigqtwzZnU+jVDkYP3Co"
|
Date: Wed, 05 Apr 2023 12:05:05 GMT
|
Connection: keep-alive
|
Keep-Alive: timeout=5
|
|
["Einkaufen"]
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers="">
|
app.get("/todo", (req, res) => {
|
return res.json(todos);
|
});
|
|
// [...]
|
|
app.post("/todo", (req, res) => {
|
// [...]
|
return res.json(todos);
|
// [...]
|
});
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p><code>GET</code> und <code>POST</code> auf klassischen Webseiten</p>
|
<pre>
|
<code class="html" data-trim data-line-numbers>
|
<form method="post" action="/todo">
|
<input name="todo"><button>add</button>
|
</form>
|
</code>
|
</pre>
|
<p>Beispiel:</p>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
cd examples/templates/006-http-web
|
node index.js
|
</code>
|
</pre>
|
</section>
|
</section>
|
<section>
|
<section>
|
<h3>JSON</h3>
|
</section>
|
<section>
|
<ul>
|
<li><em class="h">J</em>ava<em class="h">S</em>cript <em class="h">O</em>bject <em class="h">N</em>otation</li>
|
<li>Basiert auf reinem Text</li>
|
<li>Format zur strukturierten Speicherung von Daten</li>
|
<li>Attribute müssen immer in doppelte Anführungsstriche (<code>"</code>) gesetzt werden</li>
|
</ul>
|
</section>
|
<section>
|
<p>Definition eines einfachen Objekts in JS</p>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const o = {
|
x: 1,
|
y: 2
|
};
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Entsprechung in JSON</p>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
{
|
"x": 1,
|
"y": 2
|
}
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Mögliche Typen</p>
|
<p>
|
<ul>
|
<li>Number (bzw. Integer / Float)</li>
|
<li>Boolean</li>
|
<li>String</li>
|
<li>Array</li>
|
<li>Object</li>
|
</ul>
|
</p>
|
</section>
|
<section>
|
<p>Der äußere Container kann ein Array oder ein Object sein</p>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
{
|
"key": "value",
|
}
|
</code>
|
</pre>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
[
|
"foo",
|
"bar"
|
]
|
</code>
|
</pre>
|
</section>
|
</section>
|
<section>
|
<section>
|
<h2>JavaScript</h2>
|
</section>
|
<section>
|
<style>
|
table.my-table td:not(:first-child) {
|
text-align: center;
|
}
|
|
table.my-table td:not(:last-child), table.my-table th:not(:last-child) {
|
border-right: 1px solid white;
|
}
|
|
table.my-table td {
|
vertical-align: middle;
|
}
|
</style>
|
<table class="my-table">
|
<tr>
|
<th></th>
|
<th>c / c++</th>
|
<th>Java</th>
|
<th>C#</th>
|
<th>JS</th>
|
</tr>
|
<tr>
|
<td>Formale Spezifikation</td>
|
<td colspan="4" class="fragment">ja</td>
|
</tr>
|
<tr>
|
<td>Übersetzung</td>
|
<td class="fragment">Nativ kompiliert</td>
|
<td class="fragment" colspan="2">Bytecode</td>
|
<td class="fragment">Interpretiert / kompiliert (JIT)</td>
|
</tr>
|
<tr>
|
<td>Typisierung</td>
|
<td colspan="3" class="fragment">statisch</td>
|
<td class="fragment">dynamisch</td>
|
</tr>
|
<tr>
|
<td>Speicher-Management</td>
|
<td class="fragment">Manuell</td>
|
<td class="fragment" colspan="3">Garbage Collector</td>
|
</tr>
|
</table>
|
</section>
|
<section>
|
<h3>Begrifflichkeiten</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function add(x, y) {
|
return x + y;
|
}
|
|
const a = 5;
|
const b = add(a, 6);
|
const c = [];
|
</code>
|
</pre>
|
<div><span class="fragment">Operator, </span><span class="fragment">Aufruf (Call), </span><span class="fragment">Ausdruck (Expression), </span><span class="fragment">Parameter, </span><span class="fragment">Argument, </span><span class="fragment">Literal, </span></div><div><span class="fragment">Zuweisung (Assignment), </span><span class="fragment">Linke Hand (Left hand), Rechte Hand (Right hand)</span></div>
|
</section>
|
<section>
|
<h3>Vorbereiten der Beispiele (bei Bedarf)</h3>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
# im Ordner 'examples/'
|
. hooks/post-checkout
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Ausführung</h3>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
# node [Script-Datei], z.B.
|
node index.js
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Ausgaben</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
console.log("Hello World!");
|
</code>
|
</pre>
|
</section>
|
|
<section>
|
<h3>Variablen</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
// < es6
|
var x = 1;
|
|
// >= es6
|
let y = 2;
|
const z = 3;
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
var yes = false;
|
|
if (yes) {
|
var a = 1;
|
}
|
|
console.log(a);
|
</code>
|
</pre>
|
<pre class="fragment">
|
<code class="bash" data-trim data-line-numbers>
|
undefined
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
var yes = false;
|
|
if (yes) {
|
let a = 1;
|
}
|
|
console.log(a);
|
</code>
|
</pre>
|
<pre class="fragment">
|
<code class="bash" data-trim data-line-numbers>
|
ReferenceError: a is not defined
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function foo() {
|
var yes = false;
|
|
if (yes) {
|
var a = 1;
|
}
|
}
|
|
foo();
|
|
console.log(a);
|
</code>
|
</pre>
|
<pre class="fragment">
|
<code class="bash" data-trim data-line-numbers>
|
ReferenceError: a is not defined
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3><code>var</code></h3>
|
<ul>
|
<li>hat <code>function</code>-Scope</li>
|
<li>wird durch <span class="italic">hoisting</span> ("hochziehen") aus dem Block heraus an den Start der Funktion gezogen</li>
|
</ul>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
var yes = false;
|
var a;
|
|
if (yes) {
|
a = 1;
|
}
|
|
console.log(a);
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3><code>let</code>, <code>const</code></h3>
|
<ul>
|
<li>haben intuitiven Block-Scope (wie viele andere Programmiersprachen auch)</li>
|
<li>daher nur an Ort und Stelle gültig in der angegebenen Reihenfolge</li>
|
</ul>
|
</section>
|
<section>
|
<h3>Datentypen</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
let x = 5; // Number (Integer)
|
let f = 1.25; // Number (Float)
|
let b = true; // Boolean
|
let s = "hallo"; // String
|
let a = []; // Array
|
let o = {}; // Objekt
|
let nan = NaN; // Not a Number
|
let n = null; // Null
|
let u = undefined; // Undefiniert
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Alternation</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
if (x == 1) {
|
// Code
|
}
|
else if (x == 2) {
|
// alternativer Code
|
}
|
else {
|
// Fallback
|
}
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Schleifen</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
for (let i = 0; i < 5; i++) {
|
// Code
|
}
|
</code>
|
</pre>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const a = [1, 2, 3];
|
|
for (const element of a) {
|
// Code
|
}
|
</code>
|
</pre>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
while (i == 1) {
|
// Code
|
}
|
</code>
|
</pre>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
do {
|
// Code
|
} while (i == 1)
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Arrays</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
let a = [1, 2, 3];
|
|
a.push(4);
|
a.push(5);
|
|
a.pop();
|
|
console.log(a);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
[1, 2, 3, 4]
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Objekte</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
let o = {x: 1, y: 2};
|
|
console.log(o.z);
|
|
console.log(o);
|
|
o.z = 3;
|
|
console.log(o);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
undefined
|
{ x: 1, y: 2 }
|
{ x: 1, y: 2, z: 3 }
|
</code>
|
</pre>
|
</section>
|
<section>
|
<div>Objekte funktionieren intern wie Hash-Maps</div>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
let o = {};
|
|
o.a = 1;
|
o["a"] = 1; // alternativ
|
|
o["$ !"] = 2; // so sind auch komplexe Schlüssel möglich
|
|
// o."$ !" = 2; => Syntaxfehler
|
// o.$ ! = 2; => Syntaxfehler
|
|
o[true] = 3;
|
|
console.log(Object.keys(o));
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
[ 'a', '$ !', 'true' ]
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Funktionen</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function foo() {
|
console.log("foo");
|
}
|
|
function bar(a) {
|
return a;
|
}
|
|
foo();
|
|
const a = bar(1);
|
|
console.log(a);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
foo
|
1
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
let sum = function(a, b) {
|
return a + b;
|
}
|
|
let subtract = function() {
|
console.log(arguments);
|
|
return arguments[0] - arguments[1];
|
}
|
|
console.log(sum(1, 2));
|
console.log(subtract(1, 2));
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
3
|
[Arguments] { '0': 1, '1': 2 }
|
-1
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Funktionen als Konstruktor</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function Rectangle(a, b) {
|
this.a = a;
|
this.b = b;
|
}
|
|
const r = new Rectangle(2, 3);
|
|
console.log(r.a);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
2
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Objektorientierung</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function Rectangle(a, b) {
|
this.a = a;
|
this.b = b;
|
}
|
|
// jede Funktion besitzt einen Prototypen
|
Rectangle.prototype.getArea = function() {
|
return this.a * this.b;
|
}
|
|
const r1 = new Rectangle(2, 3);
|
const r2 = new Rectangle(4, 5);
|
|
console.log(r1.getArea());
|
console.log(r2.getArea());
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
6
|
20
|
</code>
|
</pre>
|
</section>
|
<section>
|
<img data-src="/assets/images/constructor-and-its-prototype.svg">
|
</section>
|
<section>
|
<div>Dynamische Änderung des Prototypen</div>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function Rectangle(a, b) {
|
this.a = a;
|
this.b = b;
|
}
|
|
const r = new Rectangle(2, 3);
|
|
console.log(r.getArea);
|
|
Rectangle.prototype.getArea = function() {
|
return this.a * this.b;
|
}
|
|
console.log(r.getArea);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
undefined
|
[Function (anonymous)]
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Aufgabe</h3>
|
Bringe dem Array eine Methode bei, welche die Summe aller enthaltenen Zahlen zurückgibt
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const a = [1, 2, 3];
|
|
console.log(a.sum());
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
6
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Lösung</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
Array.prototype.sum = function() {
|
let sum = 0;
|
|
for (let number of this) {
|
sum += number;
|
}
|
|
return sum;
|
}
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Vererbung</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers="1-13|15-18|20-29">
|
// Oberklasse
|
function Shape() {
|
}
|
|
Shape.prototype.getType = function() {
|
return "Shape";
|
}
|
|
// Unterklasse
|
function Rectangle(a, b) {
|
this.a = a;
|
this.b = b;
|
}
|
|
// Prototype der Oberklasse in die Unterklasse kopieren
|
Rectangle.prototype = Object.create(Shape.prototype);
|
// Konstruktor der Oberklasse am Prototypen durch den eigenen ersetzen
|
Rectangle.prototype.constructor = Rectangle;
|
|
const r = new Rectangle(2, 3);
|
|
console.log(r.getType());
|
|
// .getType() selber implementieren
|
Rectangle.prototype.getType = function() {
|
return "Rectangle";
|
}
|
|
console.log(r.getType());
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Shape
|
Rectangle
|
</code>
|
</pre>
|
</section>
|
<section>
|
<img data-src="/assets/images/inheritance-es5.svg">
|
</section>
|
<section>
|
<div>
|
<img width="800" height="500" data-src="/assets/images/mdn-prototype.jpg">
|
</div>
|
<p>
|
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push" style="font-size: 16px">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push</a>
|
</p>
|
</section>
|
<section>
|
<h3>Objektorientierung in es6</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
class Rectangle {
|
constructor(a, b) {
|
this.a = a;
|
this.b = b;
|
}
|
|
getArea() {
|
return this.a * this.b;
|
}
|
}
|
|
const r = new Rectangle(2, 3);
|
|
console.log(r.getArea());
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
6
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Vererbung in es6</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers="1-6|8-16|18-27">
|
// Oberklasse
|
class Shape {
|
getType() {
|
return "Shape";
|
}
|
}
|
|
// Unterklasse erbt von Oberklasse
|
class Rectangle extends Shape {
|
constructor(a, b) {
|
super();
|
|
this.a = a;
|
this.b = b;
|
}
|
}
|
|
const r = new Rectangle(2, 3);
|
|
console.log(r.getType());
|
|
// Implementierung per Prototype weiterhin möglich, falls man so will
|
Rectangle.prototype.getType = function() {
|
return "Rectangle";
|
}
|
|
console.log(r.getType());
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Shape
|
Rectangle
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Asynchronität</h3>
|
<p>Asynchron: Es wird nicht auf die Beendigung des Befehls gewartet, sondern mit dem Programm fortgefahren. Die Vollendung des asynchronen Befehls tritt <i style="font-style: italic">irgendwann</i> in der Zukunft ein.</p>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
setTimeout(callback, time); // Ausführung nach [time] Millisekunden
|
setInterval(callback, interval); // Ausführung alle [interval] Millisekunden
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
setTimeout(function() {
|
// Code, der nach 2 Sekunden ausgeführt wird
|
console.log("zweiter");
|
}, 2000);
|
|
console.log("erster");
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
erster
|
zweiter
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Aufgabe: Schreibe ein Programm, das zwei weitere beliebige Texte mit jeweils 2 Sekunden Verzögerung von einander ausgibt, d.h.</p>
|
<p>"erster" -> 2s -> "zweiter" -> 2s -> "Text A" -> 2s -> "Text B"</p>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
setTimeout(function() {
|
console.log("zweiter");
|
|
// weiterer Code
|
}, 2000);
|
|
console.log("erster");
|
</code>
|
</pre>
|
</section>
|
<section>
|
Lösung: Callback-Hell oder Pyramide des Todes
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
setTimeout(function() {
|
console.log("zweiter");
|
|
setTimeout(function() {
|
console.log("Text A");
|
|
setTimeout(function() {
|
console.log("Text B");
|
|
|
}, 2000);
|
}, 2000);
|
}, 2000);
|
|
console.log("erster");
|
</code>
|
</pre>
|
</section>
|
<section>
|
Hilfsmittel: Promises
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
new Promise(function (resolve, reject) {
|
// ich bin fertig
|
resolve();
|
|
// es ist ein Fehler aufgetreten
|
reject();
|
});
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const p = new Promise(function (resolve, reject) {...});
|
const p2 = new Promise(function (resolve, reject) {...});
|
const p3 = new Promise(function (resolve, reject) {...});
|
const p4 = new Promise(function (resolve, reject) {...});
|
|
p
|
.then(function() { return p2; })
|
.then(function() { return p3; })
|
.then(function() { return p4; });
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
console.log("erster");
|
|
delay(2000)
|
.then(function () {
|
console.log("zweiter");
|
|
return delay(2000);
|
})
|
.then(function() {
|
console.log("Text A");
|
|
return delay(2000);
|
})
|
.then(function() {
|
console.log("Text B");
|
})
|
</code>
|
</pre>
|
</section>
|
<section>
|
Aufgabe: Implementiere die Funktion "delay"
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const delay = function() {
|
// implementieren
|
}
|
|
console.log("erster");
|
|
delay(2000)
|
.then(function () {
|
console.log("zweiter");
|
|
return delay(2000);
|
})
|
.then(function() {
|
console.log("Text A");
|
|
return delay(2000);
|
})
|
.then(function() {
|
console.log("Text B");
|
})
|
</code>
|
</pre>
|
</section>
|
<section>
|
Lösung:
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const delay = function(ms) {
|
return new Promise(function(resolve, reject) {
|
setTimeout(function() {
|
resolve();
|
}, ms);
|
});
|
}
|
|
console.log("erster");
|
|
delay(2000)
|
.then(function () {
|
console.log("zweiter");
|
|
return delay(2000);
|
})
|
.then(function() {
|
console.log("Text A");
|
|
return delay(2000);
|
})
|
.then(function() {
|
console.log("Text B");
|
})
|
</code>
|
</pre>
|
</section>
|
<section>
|
Rückgabewerte mit Promises
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const p = new Promise(function(resolve, reject) {
|
resolve("ok");
|
});
|
|
p.then(function(value) {
|
console.log(value);
|
});
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
ok
|
</code>
|
</pre>
|
</section>
|
<section>
|
Fehlerbehandlung mit Promises
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const p = new Promise(function(resolve, reject) {
|
reject(new Error("Fehler"));
|
});
|
|
p
|
.then(function() {
|
console.log("Ich werde nicht aufgerufen")
|
})
|
.catch(function(e) {
|
console.log(e.message);
|
});
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Fehler
|
</code>
|
</pre>
|
</section>
|
<section>
|
Datei lesen mit NodeJS
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const fs = require("fs");
|
|
fs.readFile("./hello.txt", {encoding: "utf8"}, function(error, data) {
|
console.log(data);
|
});
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Hello World!
|
</code>
|
</pre>
|
</section>
|
<section>
|
Aufgabe: "Promisifiziere" analog zu "delay" in der letzten Aufgabe den Aufruf von fs.readFile()
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const fs = require("fs");
|
|
function readFile() {
|
// implementieren
|
}
|
|
readFile("./hello.txt").then(function(content) {
|
console.log(content);
|
});
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Hello World!
|
</code>
|
</pre>
|
</section>
|
<section>
|
Lösung:
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const fs = require("fs");
|
|
function readFile(path) {
|
return new Promise(function (resolve, reject) {
|
fs.readFile(path, {encoding: "utf8"}, function(error, data) {
|
resolve(data);
|
});
|
});
|
}
|
|
readFile("./hello.txt").then(function(content) {
|
console.log(content);
|
});
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Hello World!
|
</code>
|
</pre>
|
</section>
|
<section>
|
Aufgabe: Nutze die vorherige Lösung nun für eine Promise-Kette, sodass die Inhalte der zwei Dateien "hello.txt" und "hallo.txt" nacheinander ausgegeben werden:
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Hello World!
|
|
Hallo Welt!
|
|
</code>
|
</pre>
|
</section>
|
<section>
|
Lösung:
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const fs = require("fs");
|
|
function readFile(path) {
|
return new Promise(function (resolve, reject) {
|
fs.readFile(path, {encoding: "utf8"}, function(error, data) {
|
resolve(data);
|
});
|
});
|
}
|
|
readFile("./hello.txt")
|
.then(function(content) {
|
console.log(content);
|
|
return readFile("./hallo.txt");
|
})
|
.then(function(content) {
|
console.log(content);
|
});
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Hello World!
|
|
Hallo Welt!
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Async - Await</h3>
|
<ul>
|
<li>Syntaktischer Zucker für Promises</li>
|
<li>Nutzung von <code>await</code> nur im Kontext einer <code>async</code>-Funktion möglich</li>
|
</ul>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
async function wrapper() {
|
const p = new Promise(function(resolve, reject) {
|
resolve("Hello from Promise!");
|
});
|
|
const result = await p;
|
|
console.log(result);
|
}
|
|
wrapper();
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Hello from Promise!
|
</code>
|
</pre>
|
</section>
|
<section>
|
Aufgabe: Schreibe die Lösung der vorherigen Aufgabe nach Async - Await um
|
</section>
|
<section>
|
Lösung:
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const fs = require("fs");
|
|
function readFile(path) {
|
return new Promise(function (resolve, reject) {
|
fs.readFile(path, {encoding: "utf8"}, function(error, data) {
|
resolve(data);
|
});
|
});
|
}
|
|
async function wrapper() {
|
const content1 = await readFile("./hello.txt");
|
const content2 = await readFile("./hallo.txt");
|
|
console.log(content1);
|
console.log(content2);
|
}
|
|
wrapper();
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
Hello World!
|
|
Hallo Welt!
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Destructuring</h3>
|
<p>Auflösen von komplexen Strukturen</p>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
// Objekt destrukturieren (per Attribut-Namen)
|
const { a, b } = { a: 1, b: 2 };
|
|
console.log(a);
|
console.log(b);
|
|
// Array destrukturieren (per Position)
|
const [ c, d ] = [3, 4];
|
|
console.log(c);
|
console.log(d);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
1
|
2
|
3
|
4
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
// Bei Objekten Elemente umbenennen oder auch auslassen
|
const { a: x, b: y } = { a: 1, b: 2, c: 3 };
|
|
console.log(x);
|
console.log(y);
|
|
// Bei Arrays Elemente auslassen
|
const [ , a, b, , c] = [3, 4, 5, 6, 7, 8];
|
|
console.log(a);
|
console.log(b);
|
console.log(c);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
1
|
2
|
4
|
5
|
7
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
// Verschachteltes Destructuring
|
const {a: [, x, ] } = { a: [0, 1, 2]};
|
|
console.log(x);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
1
|
</code>
|
</pre>
|
</section>
|
<section>
|
Aufgabe: x soll die 5 enthalten
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const o = { a: [0, {b: [0, 5]}, 2]};
|
|
const { /* Destructuring-Ausdruck */ } = o;
|
|
console.log(x);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
5
|
</code>
|
</pre>
|
</section>
|
<section>
|
Lösung:
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const o = { a: [0, { b: [0, 5] }, 2]};
|
|
const { a: [, { b: [, x] }]} = o;
|
|
console.log(x);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
5
|
</code>
|
</pre>
|
</section>
|
<section>
|
Aufgabe: x soll die 3 enthalten
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const o = { a: [0, {b: [0, { c: [{ x: 5 }, { d: 3}] }]}, 2]};
|
|
const { /* Destructuring-Ausdruck */ } = o;
|
|
console.log(x);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
3
|
</code>
|
</pre>
|
</section>
|
<section>
|
Lösung:
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const o = { a: [0, {b: [0, { c: [{ x: 5 }, { d: 3}] }]}, 2]};
|
|
const { a: [, {b: [, { c: [, { d: x}] }] }] } = o;
|
|
console.log(x);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
3
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Destructuring zum parallelen Ausführen von mehreren Promises</p>
|
<pre>
|
<code class="js" data-trim data-line-numbers="1-10|11-21">
|
const fs = require("fs");
|
|
function readFile(path) {
|
return new Promise(function (resolve, reject) {
|
fs.readFile(path, {encoding: "utf8"}, function(error, data) {
|
resolve(data);
|
});
|
});
|
}
|
|
async function wrapper() {
|
const [ content1, content2 ] = await Promise.all([
|
readFile("./hello.txt"),
|
readFile("./hallo.txt")
|
]);
|
|
console.log(content1);
|
console.log(content2);
|
}
|
|
wrapper();
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Try-Catch-Finally</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function throwError() {
|
try {
|
throw new Error("Fehler!");
|
return "Hello World!";
|
}
|
catch (e) {
|
return e.message;
|
}
|
finally {
|
console.log("Finally wird immer ausgeführt");
|
return "Finally!";
|
}
|
}
|
|
const result = throwError();
|
console.log(result);
|
</code>
|
</pre>
|
<pre class="fragment">
|
<code class="bash" data-trim data-line-numbers>
|
Finally wird immer ausgeführt
|
Finally!
|
</code>
|
</pre>
|
</section>
|
<section>
|
<p>Anwendungsbeispiel für Try-Catch-Finally</p>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function readFromDatabase() {
|
try {
|
db.connect();
|
return db.read();
|
}
|
catch (e) {
|
console.log(e.stack);
|
return "";
|
}
|
finally {
|
if (db.open) {
|
db.close();
|
}
|
}
|
}
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Scope</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function a() {
|
const x = 5;
|
|
return function() {
|
return x * x;
|
};
|
}
|
|
const squareX = a();
|
const result = squareX();
|
|
console.log(result);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
25
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>this</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
class MyClass {
|
constructor(a) {
|
this.a = a;
|
}
|
|
getA() {
|
return this.a;
|
}
|
}
|
|
const x = new MyClass(5);
|
const y = new MyClass(10);
|
|
console.log(x.getA());
|
console.log(y.getA());
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
5
|
10
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
global.a = 5;
|
|
function foo() {
|
this.a += 10;
|
}
|
|
foo();
|
|
console.log(a);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
15
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
class MyClass {
|
/* ... */
|
setA() {
|
function f() {
|
this.a += 100;
|
}
|
f();
|
}
|
}
|
|
const x = new MyClass(5);
|
|
x.setA();
|
|
console.log(x.getA());
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
TypeError: Cannot read property 'a' of undefined
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
class MyClass {
|
/* ... */
|
setA() {
|
const me = this;
|
function f() {
|
me.a += 100;
|
}
|
f();
|
}
|
}
|
|
const x = new MyClass(5);
|
|
x.setA();
|
|
console.log(x.getA());
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
105
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Arrow-Funktionen</h3>
|
<ul>
|
<li>Kompakte Schreibweise einer normalen Funktion</li>
|
<li>Bei Einzeilern ist das <code>return</code> nicht notwendig</li>
|
<li>this-Pointer des aktuellen Kontextes wird beibehalten</li>
|
</ul>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
// Einzeiler
|
const f1 = _ => 1;
|
const f2 = () => 1;
|
const f3 = (x) => x * x;
|
const f4 = (x, y) => x + y;
|
|
// Mehrzeiler
|
const f5 = (x, y) => {
|
return x * y;
|
};
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
class MyClass {
|
/* ... */
|
setA() {
|
const f = () => {
|
this.a += 100;
|
}
|
f();
|
}
|
}
|
|
const x = new MyClass(5);
|
|
x.setA();
|
|
console.log(x.getA());
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
105
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Generator-Funktionen</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function* generator() {
|
let i = 0;
|
|
while(true) {
|
yield i;
|
i++;
|
}
|
}
|
|
const g = generator();
|
|
console.log(g.next().value);
|
console.log(g.next().value);
|
console.log(g.next().value);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
1
|
2
|
3
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Generator-Funktionen</h3>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
function* generator() {
|
yield 1;
|
yield 2;
|
yield 3;
|
}
|
|
const g = generator();
|
|
for (const i of g) {
|
console.log(i);
|
}
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
1
|
2
|
3
|
</code>
|
</pre>
|
</section>
|
<section>
|
<h3>Nützliche Array-Funktionen</h3>
|
</section>
|
<section>
|
Aufgabe: Schreibe ein Programm, das den Gesamtumsatz aus dem ersten Halbjahr berechnet
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const revenues = [
|
{month: 0, revenue: 1000},
|
{month: 1, revenue: 2000},
|
{month: 2, revenue: 3000},
|
{month: 3, revenue: 4000},
|
{month: 4, revenue: 5000},
|
{month: 5, revenue: 6000},
|
{month: 6, revenue: 6000},
|
{month: 7, revenue: 5000},
|
{month: 8, revenue: 4000},
|
{month: 9, revenue: 3000},
|
{month: 10, revenue: 2000},
|
{month: 11, revenue: 1000}
|
];
|
|
let totalRevenue = 0;
|
|
/* ... */
|
|
console.log(totalRevenue);
|
</code>
|
</pre>
|
</section>
|
<section>
|
Lösung
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
totalRevenue = revenues
|
.filter((revenue) => revenue.month < 6)
|
.map((revenue) => revenue.amount)
|
.reduce((sum, amount) => sum + amount, 0);
|
</code>
|
</pre>
|
<pre>
|
<code class="bash" data-trim data-line-numbers>
|
21000
|
</code>
|
</pre>
|
</section>
|
<section>
|
<pre>
|
<code class="js" data-trim data-line-numbers>
|
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
a.filter((e) => e > 2); // Filterung
|
a.map((e) => e * 2); // Umformung
|
a.reduce((p, e) => p + e, 0); // Zusammenrechnung / Reduzierung
|
a.some((e) => e > 5); // true wenn min. ein Element die Bedingung erfüllt
|
a.every((e) => e > 5); // true wenn alle Elemente die Bedingung erfüllen
|
</code>
|
</pre>
|
</section>
|
</section>
|
</div>
|
</div>
|
|
<script src="dist/reveal.js"></script>
|
<script src="plugin/notes/notes.js"></script>
|
<script src="plugin/markdown/markdown.js"></script>
|
<script src="plugin/highlight/highlight.js"></script>
|
<script>
|
// More info about initialization & config:
|
// - https://revealjs.com/initialization/
|
// - https://revealjs.com/config/
|
Reveal.initialize({
|
hash: true,
|
slideNumber: "c/t",
|
transition: "fade",
|
|
// Learn about plugins: https://revealjs.com/plugins/
|
plugins: [ RevealMarkdown, RevealHighlight, RevealNotes ]
|
});
|
</script>
|
</body>
|
</html>
|