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

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

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 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