add js examples for oop and asynchronicity
Neue Datei |
| | |
| | | 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());
|
Neue Datei |
| | |
| | | setTimeout(function() {
|
| | | // Code, der nach 2 Sekunden ausgeführt wird
|
| | | console.log("zweiter");
|
| | | }, 2000);
|
| | |
|
| | | console.log("erster");
|
Neue Datei |
| | |
| | | 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());
|
Neue Datei |
| | |
| | | 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");
|
| | | })
|
Neue Datei |
| | |
| | | // 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());
|