Hacker News new | ask | show | jobs
by dominicglenn 4634 days ago
I posted it on the page as well:

Doesn't just calling return from an event equate to returning boolean false, which means you are invoking the effects of preventDefault() and stopImmediatePropagation() thus explaining why with return it's faster as the event stops bubbling immediately?

2 comments

FWIW, the underlying jQuery code explicitly tests for false with ===:

https://github.com/jquery/jquery/blob/a5037cb9e3851b171b49f6...

So returning undefined does NOT stop propagation.

No. It equates to returning undefined, which is the same as having no return statement at all.
Although it obviously isn't quite the same. I wonder what is the test result for no return vs return true
Still faster, check revision 6 http://jsperf.com/always-return-on-jquery-events/6

revision 7 http://jsperf.com/always-return-on-jquery-events/7. Just return vs return true (faster).

But as smilekzs points out revision 3 this might not be about the return statement.

!!undefined === false
right. undefined is falsy, but it's quite different to false (i.e. undefined !== false)
true, but a boolean return value is expected for event listener callbacks.
That's not quite how it works. jQuery checks specifically for false with an === comparison. It doesn't do anything like a !! on the return value to convert it to boolean true or false. It only calls stopPropagation and preventDefault when the return value is false, not just any falsy value.

I posted the jQuery code in a previous comment; take a look at that and you can see how it works.