I haven't checked other browsers (I'm using Chrome), but it seems that the + and - operator would coerce the returned value into a number.
On Wed, May 11, 2011 at 3:21 PM, Nathan Sweet <[email protected]>wrote: > Yes there is. You could also use a "+" or "-" operator to do what the "!" > is doing, but make no mistake it isn't precisely the same thing as wrapping > the function in parentheses. The operators that do this are forcing the > function to express instead of declare (an unnamed, undeclared function is > illegal), they are doing it by coercing the function to express itself > (someone smarter than me will tell you why * and / don't work, but my guess > is that they don't coerce the same way as the operators listed above). Back > to the point at hand. If you used the ! operator to immediately invoke a > function and assigned the returned value of that function to a variable, > that value would be coerced into a boolean and reversed (ie 0 would become > true, and true would become false, false would become true, etc). Similarly > the plus operator would coerce the value into a string or number (there's > not telling which). The parentheses will NOT ever coerce the value of the > returned function. > > var g = !function(){ > return true; > } > //g equals false now. > var g = (function(){ > return true; > })(); > //g equals true. > > Hope that helps. > -Nate > > > On Wed, May 11, 2011 at 3:00 PM, tibolan <[email protected]> wrote: > >> Hi, >> >> I was just reading the source code of a script, and i found a strange >> implementation: >> >> !function (){ >> // >> }() >> >> After some test, it seems to do the same thing than: >> >> (function (){ >> // >> })(); >> >> is there another difference between this two way of doing the same >> thing, if we set aside the saving of 1 char :D ? >> >> >> -- >> To view archived discussions from the original JSMentors Mailman list: >> http://www.mail-archive.com/[email protected]/ >> >> To search via a non-Google archive, visit here: >> http://www.mail-archive.com/[email protected]/ >> >> To unsubscribe from this group, send email to >> [email protected] >> > > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
