1

I did small research regarding how the IIFE works. And stumble upon with one behavior, which I beilive related to ASI mechanism. Here is code examples which are quite self explanatory.

This block will executed successfully:

!function (){}()
^function (){}()

This not:

^function (){}()
!function (){}()

As substiturions for ! sign you may use +, - and ~. Replacements for ^ might be &, *, ,.

Could anyone give better explanation than I ended up with "this is how ASI works in JavaScript."? Which might be totally wrong, I have to admit.

Community
  • 1
  • 1
dmi3y
  • 3,482
  • 2
  • 21
  • 32
  • 6
    Um, the second example fails because ! is unary whereas ^ is binary (takes 2 operands). As for the rest, I think the linked answer covers it very well, so I'm not sure what you're unclear about. – Dave Oct 03 '13 at 19:35
  • You mean second link about ASI? Still grasping it. Yes that's sounds true for ^ takes two operands, same as & and * but what about comma? – dmi3y Oct 03 '13 at 19:39
  • 2
    Comma also takes two. You can use + and - in both places, because they have both unary and binary forms – Dave Oct 03 '13 at 19:41
  • 2
    This stuff is why Douglas Crockford insists "and don't forget your semicolons". All these many, many texts and "guides" and rules an thoughts. Just ADD THE DARN SEMICOLONS and you won't have to deal with this stuff. ASI is stupid and evil - because you are human and have to keep all these things linked here in mind, and that IN ADDITION to doing your real project. Again: just get the ; and no more issues. Yes, you CAN do it like this. Ideal for people who have no real (programming) projects and like to live in the hypothetical world :-) – Mörre Oct 03 '13 at 19:41
  • Thanks Dave, I got it now. @Mörre I always do:) Although better understanding what's behind it never hurts. – dmi3y Oct 03 '13 at 19:44
  • [Interesting update](https://github.com/styleguide/javascript) regarding adding semicolons in your script. `Do your best to never use a semicolon. This means avoiding them at line breaks and avoiding multi-statement lines. For more info, read Mislav's blog post.` I think these guys certainly have REAL programming project:) – dmi3y Mar 25 '14 at 21:22

0 Answers0