Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can write this without any problem:

  var x = function() {
    // something
  }

  // avoid polluting global scope:
  !function() {
    // Some initialization.
  }()


So now you've gone from a semicolon, which has the sole purpose of separating statements; to a bang, which doesn't make any sense in this context unless you know that it's replacing a semicolon.

Better or worse?


You could use anything which separates statements or ensures the function is treated as an expression. Two examples which are more in keeping with the intended use of the operators in question:

  ;function() {
    // Some initialization.
  }()

  void function() {
    // Some initialization.
  }()


So now we've gone a) from semicolon consistently after statements, to having to remember where to prefix with semicolons, or b) from one character to four.

Why aren't we just using the semicolon as it is required in any other (C-based) language?


JS's syntax is influenced by C via its pretensions of being somehow related to Java, but I think it's a stretch to call it a "C-based language".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: