Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda