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);
2
  • Have you actually tried it? Commented Jan 27, 2018 at 18:11
  • 1
    Yes. Doesn't work in Chrome 63 and Firefox 57. Using jQuery v1.11. No messages, no error, I don't have any clue. Commented Jan 27, 2018 at 19:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it works. I was thinking there's a simplier way to do this kind of operation.
@LuísAssunção: Not really. More: stackoverflow.com/questions/3127429/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.