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

149
Views
When would I use function borrowing?

Function borrowing is borrowing the function from an object rather than redefining it; but why should I do it. We can have a generic function and objects can use them further. To be more precise; when would I use code #1 when I can code it like in code #2:

code #1

let car1 = {
  speed: 80,
  getSpeed: function () {
    return this.speed;
  },
};
let car2 = {
  speed: 60,
};
console.log(car1.getSpeed());
console.log(car1.getSpeed.call(car2));

code #2

function getSpeed() {
  return this.speed;
}
let car1 = {
  speed: 80,
}
let car2 = {
  speed: 60,
}
console.log(getSpeed.call(car1));
console.log(getSpeed.call(car2));
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Function borrowing is used when you already have a method defined for an object (either directly or via the prototype chain), and you want to be able to use it with a similar object that doesn't have that method defined.

This is the case in your code #1 -- rather than use prototypical inheritance or classes, the getSpeed() method was defined directly in car1. Rather than duplicate the code in car2, you can borrow from car1.

You wouldn't normally design things this way from scratch. Either you would use an ordinary function as in code #2, or you would make car1 and car2 inherit from the same prototype.

Function borrowing is usually just a workaround for poor initial design. You see it in code like

nodes = document.getElementsByClassName("foo");
result = [].prototype.map.call(nodes, someFunction);

because the NodeList prototype doesn't have its own map() function.

about 4 years ago · Juan Pablo Isaza Report

0

As an addendum to Barmar's answer the third option you might consider is using a class to create object instances which share the same methods.

class Car {

  constructor(speed) {
    this.speed = speed;
  }

  getSpeed() {
    return this.speed;
  }

}

console.log(new Car(80).getSpeed());
console.log(new Car(60).getSpeed());

about 4 years ago · Juan Pablo Isaza Report

0

Benefits of using Code #1 (Function borrowing) vs Code #2 is, it lets you skip inheritance. In your example you are granting access of car1.getSpeed() to car2.

Benefits are:

  1. Skipping a need for class inheritance
  2. Reduce bugs with having 1 copy of the function vs copies floating around.

It is mostly used with native methods like slice() from Array.prototype

So if you have array like (but not exactly) structures, borrowing slice() from Array.prototype gives you access to the .map/.filter etc.

Practical example:

function findS() {
  return Array.prototype.slice.call(arguments).filter( arg => arg.includes('s'))
}

console.log(findS("Tesla", "Mitsubishi", "Ford"))

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!