Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Method borrowing vs using call()

I've started to learn about functions and this keyword. While learning about the call() and its usecase I found myself unclear of the below two scenarios which yields the same output

Method Borrowing:

const lufthansa = {
    airline: 'LUFT',
    iataCode: 500,
    bookings: [],
    book(name) {
        console.log(`${name} booked Airline: ${this.iataCode}-${this.airline}`)
        this.bookings.push({booking: `${name} booked Airline: ${this.iataCode}-${this.airline}`})
    }
}

const euroWings = {
    airline: 'EW',
    iataCode: 700,
    bookings: []
}

euroWings.book = lufthansa.book

lufthansa.book('John')
euroWings.book('Adam')

Using Call()

const lufthansa = {
    airline: 'LUFT',
    iataCode: 500,
    bookings: [],
    book(name) {
        console.log(`${name} booked Airline: ${this.iataCode}-${this.airline}`)
        this.bookings.push({booking: `${name} booked Airline: ${this.iataCode}-${this.airline}`})
    }
}

const euroWings = {
    airline: 'EW',
    iataCode: 700,
    bookings: []
}

lufthansa.book('John')
lufthansa.book.call(euroWings, 'Adam')

What are the real differences and when one should be used?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

.call() is generally used when you don't control the methods of the object, and want to use methods from some other object.

The classical example is using array methods on "array-like" objects that exist in the web interface, such as NodeList.

divs = document.getElementByClassName("myclass");
result = [].map.call(divs, function(div) { return div.getAttribute("someattr"); });

You could conceivably do

div.map = [].map;
result = div.map(function(div) { return div.getAttribute("someattr"); });

but borrowing the method every time would be repetitive.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!