Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

344
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!