diff --git a/javascript/Javascript Expert/chapter10/applyAndCallMethod.js b/javascript/Javascript Expert/chapter10/applyAndCallMethod.js new file mode 100644 index 0000000..eda04bc --- /dev/null +++ b/javascript/Javascript Expert/chapter10/applyAndCallMethod.js @@ -0,0 +1,57 @@ +const lufthansa = { + airline: "Lufthansa", + iataCode: "LH", + bookings: [], + // neue Syntax (ES2020) einer Methode eines Objektes + book(flightNumber, passengerName) { + console.log( + `${passengerName} booked a seat on ${this.iataCode}${flightNumber}` + ); + this.bookings.push({ + flight: `${this.iataCode}${flightNumber}`, + passengerName, + }); + }, +}; + +lufthansa.book(573, "David Aster"); +lufthansa.book(910, "Joanne Aster"); +console.log(lufthansa.bookings); + +const eurowings = { + airline: "Eurowings", + iataCode: "EW", + bookings: [], +}; +// book Funktion gehört nicht mehr zum lufthansa Objekt, sondern ist eine selbständige/eigene Funktion +const book = lufthansa.book; +// eurowings.book = book; + +// die Methode call ruft eigentlich book Methode mit zwei Argumenten (226, "David Aster") auf, und weise diese Methode zum Objekt eurowings zu +// (call Methode zeigt auf das eurowings Objekt) +// Ausgabe wird "David Aster booked a seat on EW226" sein +book.call(eurowings, 226, "David Aster"); +// Ausgabe wird "Joanne Aster booked a seat on EW897" sein +book.call(eurowings, 897, "Joanne Aster"); + +// checken, ob die hinzugefügten Objekten im Feld bookings existieren +console.log(eurowings.bookings); + +const swiss = { + airline: "Swiss Air Lines", + iataCode: "SW", + bookings: [], +}; + +// wiederverwendbare book Funktion in jedem Objekt +book.call(swiss, 998, "David Aster"); + +// apply Methode, bekommt statt eines einzelnen Wert als Parameter, ein Feld der Werte +let fligtData = [669, "David Aster"]; +// apply Methode ist veraltete Methode und ist es nichts mehr oft benutzt +book.apply(swiss, fligtData); +console.log(swiss.bookings); + +// stattdessen benutzt man eine Kombination der call methode und des spread Operatoren +fligtData = [109, "David Schmidt"]; +book.call(swiss, ...fligtData); diff --git a/javascript/Javascript Expert/chapter10/firstClassFunctions.js b/javascript/Javascript Expert/chapter10/firstClassFunctions.js new file mode 100644 index 0000000..20eda0e --- /dev/null +++ b/javascript/Javascript Expert/chapter10/firstClassFunctions.js @@ -0,0 +1,41 @@ +const add = (a, b) => a + b; +const counter = { + value: 23, + inc: function () { + this.value + 1; + }, +}; +const greet = () => { + console.log("Hey David"); +}; + +// const btnGreet = document.getElementById("greet"); +// btnGreet.addEventListener("click", greet); + +// eine Funktion, die eine Funktion zurück gibt +const count = function () { + let counter = 0; + return function () { + console.log("TEST"); + }; +}; +count()(); + +const oneWord = function (str) { + return str.replace(/ /g, "").toLowerCase(); +}; + +const upperFirstWord = function (str) { + const [firstWord, ...others] = str.split(" "); + return [firstWord.toUpperCase(), ...others].join(" "); +}; + +// Higher-order function +const transformer = function (str, fn) { + console.log(`Original string: ${str}`); + console.log(`Transformed string: ${fn(str)}`); + console.log(`Transformed by: ${fn.name}`); +}; + +transformer("JavaScript is the best", upperFirstWord); +transformer("JavaScript is the best", oneWord); diff --git a/javascript/Javascript Expert/chapter10/functionReturnFunction.js b/javascript/Javascript Expert/chapter10/functionReturnFunction.js new file mode 100644 index 0000000..122fa49 --- /dev/null +++ b/javascript/Javascript Expert/chapter10/functionReturnFunction.js @@ -0,0 +1,16 @@ +const greet = function (greeting) { + return function (name) { + console.log(`${greeting} ${name}`); + }; +}; + +// greeterHey wird jetzt Methode werden, weil die Methode greet gibt eine Methode zurück +const greeterHey = greet("Hey"); +// jetzt kann man Methode die unter Methode greet steht ausführen +greeterHey("David"); +// es gibt die Möglichkeit die beiden Methoden auf einmal auszuführen +greet("Hej")("David"); + +// das Gleiche wie die obene Methode greet +const greetArrow = greeting => name => console.log(`${greeting} ${name}`); +greetArrow("Hej")("Joanne"); diff --git a/javascript/Javascript Expert/chapter10/index.html b/javascript/Javascript Expert/chapter10/index.html index 2437d9c..da237fa 100644 --- a/javascript/Javascript Expert/chapter10/index.html +++ b/javascript/Javascript Expert/chapter10/index.html @@ -4,8 +4,10 @@ -