58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
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);
|