From a8783a284efd0a105d8b12f88d760df9ec97d03d Mon Sep 17 00:00:00 2001
From: Sascha Schulz <sschulz@dh-software.de>
Date: Di, 25 Apr 2023 16:09:46 +0200
Subject: [PATCH] add examples for functions, constructors, objects and inheritance

---
 templates/007-js/objects.js      |   13 +++++++++++++
 templates/007-js/functions-1.js  |    0 
 templates/007-js/constructors.js |   11 +++++++++--
 templates/007-js/functions-2.js  |   12 ++++++++++++
 templates/007-js/inheritance.js  |   24 ++++++++++++++++++++++++
 5 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/templates/007-js/constructors.js b/templates/007-js/constructors.js
index e7714df..2d008ad 100644
--- a/templates/007-js/constructors.js
+++ b/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());
diff --git a/templates/007-js/functions.js b/templates/007-js/functions-1.js
similarity index 100%
rename from templates/007-js/functions.js
rename to templates/007-js/functions-1.js
diff --git a/templates/007-js/functions-2.js b/templates/007-js/functions-2.js
new file mode 100644
index 0000000..40d1e86
--- /dev/null
+++ b/templates/007-js/functions-2.js
@@ -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));
diff --git a/templates/007-js/inheritance.js b/templates/007-js/inheritance.js
new file mode 100644
index 0000000..c48224f
--- /dev/null
+++ b/templates/007-js/inheritance.js
@@ -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());
diff --git a/templates/007-js/objects.js b/templates/007-js/objects.js
new file mode 100644
index 0000000..27271fe
--- /dev/null
+++ b/templates/007-js/objects.js
@@ -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));

--
Gitblit v1.9.3