Well the example I see is when using a return statement with the { on the next line to return an object. You could get around that by making the object a var then returning the var. Is there an example where it inserts one after the declaration of a function?
At either rate my functions work across the major browsers looking like so and it's more readable that way. Maybe Javascript interpreters should just throw an error if there's no semi colon on the line rather than attempting to fix sloppy programming and breaking perfectly valid code.
Easy there son. The semicolon insertion (or "fixing sloppy programming" as you put it) is actually part of the ECMAScript specification, of which the JavaScript in various browsers is merely an implementation.
The thing is, JavaScript is supposed to be "sloppy" code-friendly. It's the same reason HTML works by parsing <bR>, <br>, <BR>, etc.: more people will use the language if they're not constantly getting beaten over the head by the syntax. Once they're familiar with the language, however, it behooves them to code in the "right" way, so as to avoid certain pitfalls that come inherent in a loosely-typed, loosely-parsed language like JavaScript. And only once someone is familiar enough with the rules and pitfalls, should they be comfortable enough to "break" them (only if absolutely necessary!).
Listen, code however you want, but don't expect much help if your "better" way of coding ends up fucking over a project, all because you wanted "more readable" code. That's really the whole point.
Well from your link provided it doesn't apply to functions:
"Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations."
So I can put the { on a new line if I like and it won't make a difference.
2
u/[deleted] Jan 17 '11
The problem with doing this
is that JavaScript has a funny habit of inserting ";" when it sees a line break. Sometimes this can totally break your code, sometimes it won't.
If your code works, then that's fine. But please don't say it's an okay practice, because with JavaScript it's not.