const lufthanza = { airline: "Lufthanza", iataCode: "LH", bookings: [], book: function (flightNumber, name) { this.bookings.push({ flightCode: flightNumber, passenger: name }); return `${name} booked a flight with this ${this.iataCode} on ${this.airline} with this ${flightNumber}`; }, }; console.log(lufthanza); console.log(lufthanza.book("KIG808", "David")); //second object without the function BUT it has the same keys const etihad = { airline: "Etihad", iataCode: "EH", bookings: [], }; // create the variable for the book function const bookingCalc = lufthanza.book; // create the call on the object that needs that function bookingCalc.call(etihad, "HLSMSJ", "John"); // using call() to spread into the bookings array from the existing call const passengerInfo = ['gktu78', 'Kevin'] bookingCalc.call(etihad, ...passengerInfo); console.log(etihad)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter