4 ms·
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 preve
by dominicglenn 13y 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?
- EmielMols 13y agoNo. It equates to returning undefined, which is the same as having no return statement at all.
- dreen 13y agoAlthough it obviously isn't quite the same. I wonder what is the test result for no return vs return true
- Tarang 13y agoStill faster, check revision 6 http://jsperf.com/always-return-on-jquery-events/6 http://jsperf.com/always-return-on-jquery-events/6 revision 7 http://jsperf.com/always-return-on-jquery-events/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.
- lucideer 13y ago!!undefined === false
- bengillies 13y agoright. undefined is falsy, but it's quite different to false (i.e. undefined !== false)
- lucideer 13y agotrue, but a boolean return value is expected for event listener callbacks.
- Stratoscope 13y agoThat'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.
- al2o3cr 13y agoFWIW, the underlying jQuery code explicitly tests for false with ===: https://github.com/jquery/jquery/blob/a5037cb9e3851b171b49f6d717fb40e59aa344c2/src/event.js#L317 https://github.com/jquery/jquery/blob/a5037cb9e3851b171b49f6... So returning undefined does NOT stop propagation.