0

My code is this:

$('button#submit-order').on('click', () => orderPanelInfo.hide());

I'm wondering if it's possible to do same operation like this:

$('button#submit-order').on('click', orderPanelInfo.hide);
Luís Assunção
  • 773
  • 1
  • 9
  • 19

1 Answers1

1

What you're doing (the arrow function) is fine provided the browsers you're targeting support them (otherwise, just use a normal function). The other alternative is to use Function#bind but it involves repeating yourself:

$('button#submit-order').on('click', orderPanelInfo.hide.bind(orderPanelInfo));
// -----------------------------------------------------^^^^^^^^^^^^^^^^^^^^^

Function#bind was added in 2009 so only Really Truly Obsolete browsers have JavaScript engines that don't have it (and it can be polyfilled on them if necessary).

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769