3 ms·
Nope. 1 is not a property name that can be used with the dotted notation because it begins with a figure. foo.bar().1 + baz() gives: Uncaught SyntaxError:
by jraph 4y ago
Nope. 1 is not a property name that can be used with the dotted notation because it begins with a figure.
foo.bar().1 + baz() gives:
Uncaught SyntaxError: unexpected token: numeric literal
The following:
foo.bar()
.1 + baz()
is equivalent to:
foo.bar();
.1 + baz();
The parser will find this syntax error, backtrack and insert a semicolon (Automatic Semicolon Insertion applies).
Interestingly, I tried to run the following in the browser console:
{}.1
And it runs. It gives 0.1. This is probably because {} is interpreted as an empty block (of statements), I guess. The following gives the same result:
{};.1
The following is a parse error:
({}).1
I tried the following to convince myself that {} is evaluated as an empty block, so I went further.
The following works:
{a:2}.1
... because a: is a label. you really need to try with "two properties" to see a syntax error:
{a:2, b:2}.1
Gives: Uncaught SyntaxError: unexpected token: ':' (character 7). A label cannot be introduced after the comma operator... (yes, because it is a comma operator! {a:2, console.log("hello")}.1 prints "hello" and its result is 0.1)
(WAT?)
edit: {;}.1 and {;;}.1 also work of course.
- littlestymaar 4y ago> Nope. 1 is not a property name that can be used with the dotted notation because it begins with a figure. Oops, you're right.