diff --git a/javascript/Javascript Expert/chapter10/ImmediatelyInvokedFunctionExpression.js b/javascript/Javascript Expert/chapter10/ImmediatelyInvokedFunctionExpression.js new file mode 100644 index 0000000..b0110d1 --- /dev/null +++ b/javascript/Javascript Expert/chapter10/ImmediatelyInvokedFunctionExpression.js @@ -0,0 +1,11 @@ +// const runOnce = function () { +// console.log("This will never run again"); +// }; + +// eine solche Methode kann man nicht manuel ausführen, sie wird noch einmal bei dem Laden des Scripts ausgeführt +// Ideal für die ursprünglichen Einstellungen der Resourcen einer WEB-Applikation (IIFE-Immediatelly Invoked Function Expression) +(function () { + console.log("This will never run again"); +})(); + +() => console.log("This will also never run again")(); diff --git a/javascript/Javascript Expert/chapter10/bindMethod.js b/javascript/Javascript Expert/chapter10/bindMethod.js new file mode 100644 index 0000000..8b63ea9 --- /dev/null +++ b/javascript/Javascript Expert/chapter10/bindMethod.js @@ -0,0 +1,68 @@ +const lufthansa = { + airline: "Lufthansa", + iataCode: "LH", + bookings: [], + + book: function (flightNumber, passengerName) { + console.log( + `${passengerName} booked a seat on ${this.iataCode}${flightNumber}` + ); + this.bookings.push({ + flight: `${this.iataCode}${flightNumber}`, + passengerName, + }); + }, +}; + +const book = lufthansa.book; + +const wizzair = { + airline: "Wizzair Air Hungary", + iataCode: "WZ", + bookings: [], +}; + +// binding der Methode book aus dem lufthasa Objekt dem wizzair Objekt +const bookEW = book.bind(wizzair); +// Verwendung der book Methode im wizzair Objekt, und ihre Ausführung +bookEW(663, "David A."); + +console.log(wizzair.bookings); + +const bookWZ52 = book.bind(wizzair, 52); +bookWZ52("David Aster"); +bookWZ52("Joanne Aster"); +console.log(wizzair.bookings); + +lufthansa.planes = 300; +lufthansa.buyPlane = function () { + console.log(this); + this.planes++; + + console.log(this.planes); +}; + +// console.log(lufthansa.planes); +lufthansa.buyPlane(); + +// document +// .querySelector(".buy") +// .addEventListener("click", lufthansa.buyPlane.bind(lufthansa)); + +// Partial Applikation +const addTAX = (rate, value) => value + rate * value; +const addVAT = addTAX.bind(null, 0.22); +// addTAX = (rate, value) => value + 0.22 * value; +// Mehrwertsteuer wird da immer 22% sein +console.log(addVAT(120)); + +// Das Gleiche kann man auch mit einer Methode erreichen, die eine Methode zurück gibt +const addTAX1 = function (rate) { + return function (value) { + return value + value * rate; + }; +}; + +const addVAT1 = addTAX1(0.22); +console.log(addVAT1(100)); +console.log(addVAT1(120)); diff --git a/javascript/Javascript Expert/chapter10/closures.js b/javascript/Javascript Expert/chapter10/closures.js new file mode 100644 index 0000000..ba346bc --- /dev/null +++ b/javascript/Javascript Expert/chapter10/closures.js @@ -0,0 +1,27 @@ +const secureBooking = function () { + let passengerCount = 0; + return function () { + passengerCount++; + console.log(`${passengerCount} passengers`); + }; +}; + +const booker = secureBooking(); +booker(); +booker(); +booker(); + +// console.dir(secureBooking); + +const boardPassengers = function (n, wait) { + const perGroup = n / 3; + + setTimeout(function () { + console.log(`We are now boarding all ${n}`); + console.log(`There are 3 groups, each with ${perGroup}`); + }, wait * 1000); + console.log(`Will start boarding in ${wait} seconds`); +}; + +const perGroup = 1000; +boardPassengers(180, 3); diff --git a/javascript/Javascript Expert/chapter10/index.html b/javascript/Javascript Expert/chapter10/index.html index da237fa..928589d 100644 --- a/javascript/Javascript Expert/chapter10/index.html +++ b/javascript/Javascript Expert/chapter10/index.html @@ -4,10 +4,10 @@ - Callbacks + Closer look at functions - - + + diff --git a/javascript/Javascript Expert/chapter10/package.json b/javascript/Javascript Expert/chapter10/package.json new file mode 100644 index 0000000..3dbc1ca --- /dev/null +++ b/javascript/Javascript Expert/chapter10/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +}