Re:Always return on events is faster, but why?
by EmielMols on Friday October 18, 2013 @05:51AM
(#6570532)
No. It equates to returning undefined, which is the same as having no return statement at all.
[ Parent
| on HN ]
Re:Always return on events is faster, but why?
by dreen on Friday October 18, 2013 @05:53AM
(#6570544)
Although it obviously isn't quite the same. I wonder what is the test result for no return vs return true
Re:Always return on events is faster, but why?
by Tarang on Friday October 18, 2013 @06:00AM
(#6570564)
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.
Re:Always return on events is faster, but why?
by lucideer on Friday October 18, 2013 @05:59AM
(#6570558)
!!undefined === false
Re:Always return on events is faster, but why?
by bengillies on Friday October 18, 2013 @06:03AM
(#6570580)
right. undefined is falsy, but it's quite different to false (i.e. undefined !== false)
Re:Always return on events is faster, but why?
by lucideer on Friday October 18, 2013 @07:00AM
(#6570718)
true, but a boolean return value is expected for event listener callbacks.
Re:Always return on events is faster, but why?
by Stratoscope on Friday October 18, 2013 @12:54PM
(#6572398)
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.