Sascha Schulz
2023-05-09 0539670917d0aeb39caa988aa1fe05c8a0104dab
add js examples for oop and asynchronicity
5 Dateien hinzugefügt
80 ■■■■■ Geänderte Dateien
templates/007-js/array-sum.js 13 ●●●●● Patch | Ansicht | Raw | Blame | Historie
templates/007-js/async-1.js 6 ●●●●● Patch | Ansicht | Raw | Blame | Historie
templates/007-js/constructors-es6.js 14 ●●●●● Patch | Ansicht | Raw | Blame | Historie
templates/007-js/delay.js 20 ●●●●● Patch | Ansicht | Raw | Blame | Historie
templates/007-js/inheritance-es6.js 27 ●●●●● Patch | Ansicht | Raw | Blame | Historie
templates/007-js/array-sum.js
Neue Datei
@@ -0,0 +1,13 @@
const a = [1, 2, 4, 8];
Array.prototype.sum = function() {
    let sum = 0;
    for (let number of this) {
        sum += number;
    }
    return sum;
}
console.log(a.sum());
templates/007-js/async-1.js
Neue Datei
@@ -0,0 +1,6 @@
setTimeout(function() {
    // Code, der nach 2 Sekunden ausgeführt wird
    console.log("zweiter");
}, 2000);
console.log("erster");
templates/007-js/constructors-es6.js
Neue Datei
@@ -0,0 +1,14 @@
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());
templates/007-js/delay.js
Neue Datei
@@ -0,0 +1,20 @@
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");
    })
templates/007-js/inheritance-es6.js
Neue Datei
@@ -0,0 +1,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());