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

350
Vistas
Is there any difference betwwen these two approaches(bind a function to button with parameter) in JavaScript?

Here is the detail:

The first one:

someButton.addEventListener('click', function() {
  greet('Max'); 
});

The second one:

someButton.addEventListener('click', greet.bind(null, 'Max')); 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's vanishingly unlikely, but it's theoretically possible for there to be a difference in behavior.

In strict mode, this doesn't have to be an object. If you do greet.bind(null, the this will be null - and if you do greet(), the this will be undefined.

'use strict';
const someButton = document.querySelector('.btn');
function greet() {
  console.log(this);
}
someButton.addEventListener('click', function() {
  greet('Max'); 
});
someButton.addEventListener('click', greet.bind(null, 'Max'));
<button class="btn">click</button>

But for any even halfway reasonable code, this difference would not be something anyone should be testing - in which case those two approaches are identical for all intents and purposes.

Though, since it looks like greet isn't intending to use any this, you could consider just not modifying it at all, and just pass it as the handler itself:

someButton.addEventListener('click', greet);

which I'd consider to be perfectly reasonable (and, importantly, a bit easier to make sense of at a glance).

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