call()

PHOTO EMBED

Fri Jul 12 2024 02:13:42 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #call() #object

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)
content_copyCOPY