!!! Listings aus dem Artikel "Klassentreffen" von Stefan Mintert in !!! iX 8/2015, S. 123 !!! Listing 1 @ü.li:Listing 1: Neues Schlüsselwort let function declaredWithVar() { var x = 1; if (x > 0) { var x = 2; // gleiche Variable! console.log(x); // 2 } console.log(x); // 2 } function declaredWithLet() { var x = 1; if (x > 0) { let x = 2; // eigener Scope console.log(x); // 2 } console.log(x); // 1 } function declaredWithLateVar() { console.log(x); // undefined if (true) { var x = 2; // eigener Scope console.log(x); // 2 } var x = 1; console.log(x); // 1 } function undeclared() { console.log(x); // Fehler und Programmabbruch if (true) { console.log(x); // 2 } console.log(x); // 1 } console.log('// declaredWithVar'); declaredWithVar(); console.log('// declaredWithLet'); declaredWithLet(); console.log('// declaredWithLateVar'); declaredWithLateVar(); console.log('// undeclared'); undeclared(); !!! Listing 2 @ü.li:Listing 2: Vererbung mit Prototyping function Vehicle() { var gear = 0; this.upshift = function() { gear++; return gear; } this.downshift = function() { gear = Math.max(gear--, 0); return gear; } this.getGear = function() { return gear; } } function Bicycle() { this.numberOfWheels = 2; } Bicycle.prototype = new Vehicle(); function Car() { this.numberOfWheels = 4; } Car.prototype = new Vehicle(); var a = new Bicycle(); console.log(a.upshift()); // 1 console.log(a.getGear()); // 1 console.log(a.numberOfWheels); // 2 var b = new Car(); console.log(b.upshift()); // 1 console.log(b.getGear()); // 1 console.log(b.numberOfWheels); // 4 !!! Listing 3 @ü.li:Listing 3: Vererbung mit Klassen class Vehicle { constructor() { this.currentGear = 0; } upshift() { this.currentGear++; return this.currentGear; } downshift() { currentGear = Math.max(this.currentGear--, 0); return this.currentGear; } set gear(g) { this.currentGear = g; } get gear() { return this.currentGear; } } class Car extends Vehicle { constructor() { super(); this.numberOfWheels = 4; } } class Bicycle extends Vehicle { constructor() { super(); this.numberOfWheels = 2; } } var a = new Bicycle(); console.log(a.upshift()); // 1 console.log(a.gear); // 1 console.log(a.numberOfWheels); // 2 var b = new Car(); console.log(b.upshift()); // 1 console.log(b.gear); // 1 console.log(b.currentGear); // 1 console.log(b.numberOfWheels); // 4 !!! Listing 4 @ü.li:Listing 4: Potenzfunktion mit Array aufrufen function pow(b, e=2) { return Math.pow(b, e); } function powArray(basis, ...expArray) { return expArray.map(function(exp) { return pow(basis,exp); }); } console.log(powArray(2,0,1,2,3,4,5,6,7,8,9,10)); // [ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 ] !!! Listing 5 @ü.li:Listing 5: Zwei Adressen let addressA = { name: "Max Mustermann", street: "Haupstrasse 5", zip: "01234", city: "Berlin" }; let addressB = { name: "Moritz Mustermann", street: "Praterstraße 1", zip: "1180", city: "Wien", country: "Österreich" }; // Konsolenausgabe console.log(proxiedAddressA.country); // Deutschland console.log(proxiedAddressB.country); // Österreich !!! bitte Linie ziehen // zwei let-Statements let proxiedAddressA = new Proxy(addressA, addressHandler); let proxiedAddressB = new Proxy(addressB, addressHandler); !!! Listing 6 @ü.li:Listing 6: Proxy-Handler let addressHandler = { get(originalObject, property) { console.log("GET " + property); if (originalObject[property]) { // (1) return originalObject[property]; } else if (property == "country") { // (2) return "Deutschland"; } else { return; // (3) } } }; !!! Listing 7 @ü.li:Listing 7: Promise: Versprechen geben function fragSpaeter(verzug = 1000, frage) { return new Promise(function(fulfill, reject) { console.log("// 1"); setTimeout(function() { console.log("// 2"); let antwort = confirm(frage); if (antwort) { fulfill("Du hast Ja gesagt"); } else { fulfill("Du hast Nein gesagt"); } }, verzug) }); } !!! Listing 8 @ü.li:Listing 8: fragSpaeter: neues Versprechen fragSpaeter(5000,'Ja oder Nein?').then(function(result) { console.log("Fulfilled: " + result); // "Fulfilled: Du hast Ja gesagt" }, function(err) { console.log("Rejected: " + err); // "Rejected: Du hast Nein gesagt" }); console.log("// 3"); !!! Listing 9 @ü.li:Listing 9: Endrekursion function summeA(x, sum=0) { let newX = 0; if (x < 1) { return sum; } newX = summeA(x - 1, sum + x); return newX; }