Indeed, ! + - are all unary operators. The reason they are used is that a
statement may not start with the function keyword because that's reserved
for function declarations (which you can't immediately invoke because
function declarations don't have a return value).
So when you want to do the local scope module kind of thing, you usually see
these (function ....{})() patterns a lot. The "extra" parens are to make
sure the line doesn't start with the keyword. For all intentions and
purposes, that's all they do.
Replacing the extra parens with a unary is indeed to save one byte. The
result of the function expression is converted to a number or boolen and
then... ignored. So it doesn't matter and the extra computation is typically
neglectable.
I sometimes use "new" because you can leave out the calling parens if
there's no args to pass on ;) like "new function(){...}" and we still don't
care about the return type :)
Anyways, that's the background on these function expressions.
- peter
On 12 May 2011 01:12, "Nathan Sweet" <[email protected]> wrote:
>
> Jacob,
> Thanks for your explanation. I never thought about it that way before, but
it makes complete sense.
>
>
> On Wed, May 11, 2011 at 3:35 PM, Jacob Beard <[email protected]> wrote:
>>
>> On Wed, May 11, 2011 at 6:21 PM, Nathan Sweet <[email protected]>
wrote:
>> > (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).
>>
>> I think that +, -, and ! can be unary or binary operators in js
>> syntax, whereas * and / can only be binary operators. This makes sense
>> when you think about their meaning in mathematics.
>>
>> Best,
>>
>> Jake
>>
>> --
>> 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]
--
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]