Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda