add examples for functions, constructors, objects and inheritance
1 Dateien umbenannt
1 Dateien geändert
3 Dateien hinzugefügt
| | |
| | | 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());
|
Neue Datei |
| | |
| | | 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));
|
Neue Datei |
| | |
| | | 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());
|
Neue Datei |
| | |
| | | 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));
|