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
  • 771
  • 1
  • 8
  • 20

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
  • 1,031,962
  • 187
  • 1,923
  • 1,875