On Wed, Jan 12, 2011 at 4:25 AM, Peter van der Zee <[email protected]> wrote:
> On Wed, Jan 12, 2011 at 7:07 AM, Nicholas C. Zakas <[email protected] > > wrote: > >> ?To be more precise void is an operator, just typeof. The parentheses are >> optional, just like you can write -1 or -(1), the same is true for void(0) >> or void 0, although white space is required when parentheses aren't there >> for void and typeof. >> > > Ah, there's an interesting "quirk" (?) in the language there. Did you know > it's not actually required? You're required to let the parser be able to > distinct the keyword from an identifier. This goes for all > literals/operators. If the parser is able to do that, then the whitespace is > not needed. These are all valid statements, which do exactly as you'd expect > them to. > I remember being confused about this too. But it becomes pretty clear once you realize that whitespace merely serves as tokens separator. It even says so in 7.2: *White space characters are used to improve source text readability and to separate tokens (indivisible lexical units) from each other, but are otherwise insignificant. White space characters may occur between any two tokens and at the start or end of input.* So `typeof(5)` can be unambiguously parsed as `typeof` operator applied to `(5)` expression. So can `typeof 5`, obviously. On the other hand, `typeof5` can't, since it now matches identifier production (and program will try to evaluate + resolve that reference). Other curiously valid productions are `return'foo'` and `return-5`; first one is a return statement that has "foo" string literal in its expression, and second one is a return statement that has `-5` numeric literal in its expression. A related discussion on "return and whitespace" — https://groups.google.com/group/comp.lang.javascript/browse_thread/thread/eb969b7d402b3078/ [snip examples] -- kangax -- 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]
