
I think this is the way it is typically used here and elsewhere. reserving "brackets" without a qualifier for indicating square brackets., so probably no explanation is necessary. But, for those who want to know more: It does seem to be the case that in British English "brackets" typically means "round brackets", which are parentheses, but in American English "brackets" typically means square brackets ("[]"). Admittedly, "brackets" is easier to spell. And to pronounce. Even to type, once you get used to it.
37 lines
1.1 KiB
Markdown
37 lines
1.1 KiB
Markdown
**Error**!
|
|
|
|
Try it:
|
|
|
|
```js run
|
|
let user = {
|
|
name: "John",
|
|
go: function() { alert(this.name) }
|
|
}
|
|
|
|
(user.go)() // error!
|
|
```
|
|
|
|
The error message in most browsers does not give us much of a clue about what went wrong.
|
|
|
|
**The error appears because a semicolon is missing after `user = {...}`.**
|
|
|
|
JavaScript does not auto-insert a semicolon before a bracket `(user.go)()`, so it reads the code like:
|
|
|
|
```js no-beautify
|
|
let user = { go:... }(user.go)()
|
|
```
|
|
|
|
Then we can also see that such a joint expression is syntactically a call of the object `{ go: ... }` as a function with the argument `(user.go)`. And that also happens on the same line with `let user`, so the `user` object has not yet even been defined, hence the error.
|
|
|
|
If we insert the semicolon, all is fine:
|
|
|
|
```js run
|
|
let user = {
|
|
name: "John",
|
|
go: function() { alert(this.name) }
|
|
}*!*;*/!*
|
|
|
|
(user.go)() // John
|
|
```
|
|
|
|
Please note that parentheses around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
|