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

233
Vistas
How to bind an object to a function in JavaScript?

I have the next task:

const person = {name: 'Arnold'};

function fn(salut, lastName) {
    console.log(salut, this.name, lastName);
}

const bindName = bind(fn, person, 'Mr');

bindName('Helgov');

I have to write a function that prints: "Mr Arnold Helgov"

My realization of this task is the next function:

function bind(fn, person, salute) {
    return (lastname) => {
        return fn(salute, lastname);
    }
}

But I have a problem here. My function prints only "Mr Helgov" and the name 'Arnold' is lost. How to solve this problem (maybe by using bind or apply or call)?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

How to solve this problem (maybe by using bind or apply or call)?

Yes, you can use call or bind to make sure this gets to be equal to person during the call of fn.

With call:

const person = {name: 'Arnold'};

function fn(salut, lastName) {
    console.log(salut, this.name, lastName);
}

const bindName = bind(fn, person, 'Mr');

bindName('Helgov');

function bind(fn, person, salute) {
    return (lastname) => {
        return fn.call(person, salute, lastname);
    }
}

Or, bind:

const person = {name: 'Arnold'};

function fn(salut, lastName) {
    console.log(salut, this.name, lastName);
}

const bindName = bind(fn, person, 'Mr');

bindName('Helgov');

function bind(fn, person, salute) {
    return fn.bind(person, salute);
}

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