Sascha Schulz
2023-04-25 a8783a284efd0a105d8b12f88d760df9ec97d03d
add examples for functions, constructors, objects and inheritance
1 Dateien umbenannt
1 Dateien geändert
3 Dateien hinzugefügt
60 ■■■■■ Geänderte Dateien
templates/007-js/constructors.js 11 ●●●● Patch | Ansicht | Raw | Blame | Historie
templates/007-js/functions-1.js Patch | Ansicht | Raw | Blame | Historie
templates/007-js/functions-2.js 12 ●●●●● Patch | Ansicht | Raw | Blame | Historie
templates/007-js/inheritance.js 24 ●●●●● Patch | Ansicht | Raw | Blame | Historie
templates/007-js/objects.js 13 ●●●●● Patch | Ansicht | Raw | Blame | Historie
templates/007-js/constructors.js
@@ -3,6 +3,13 @@
    this.b = b;
}
const r = new Rectangle(2, 3);
// jede Funktion besitzt einen Prototypen
Rectangle.prototype.getArea = function() {
    return this.a * this.b;
}
console.log(r.a);
const r1 = new Rectangle(2, 3);
const r2 = new Rectangle(4, 5);
console.log(r1.getArea());
console.log(r2.getArea());
templates/007-js/functions-1.js
templates/007-js/functions-2.js
Neue Datei
@@ -0,0 +1,12 @@
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));
templates/007-js/inheritance.js
Neue Datei
@@ -0,0 +1,24 @@
function Shape() {
}
Shape.prototype.getType = function() {
    return "Shape";
}
function Rectangle(a, b) {
    this.a = a;
    this.b = b;
}
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
const r = new Rectangle(2, 3);
console.log(r.getType());
Rectangle.prototype.getType = function() {
    return "Rectangle";
}
console.log(r.getType());
templates/007-js/objects.js
Neue Datei
@@ -0,0 +1,13 @@
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));