Skip to content

Commit b903e95

Browse files
Add another cause for unexpected token error (#36624)
* add method property Confusing error if a method name is not followed by `()` but by `:` * Update index.md * Update index.md --------- Co-authored-by: Joshua Chen <[email protected]>
1 parent 93a294e commit b903e95

File tree

1 file changed

+22
-0
lines changed
  • files/en-us/web/javascript/reference/errors/unexpected_token

1 file changed

+22
-0
lines changed

files/en-us/web/javascript/reference/errors/unexpected_token/index.md

+22
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ function round(n, upperBound, lowerBound) {
9090
}
9191
```
9292

93+
### A structure error further up confused the meaning
94+
95+
Sometimes, the error is caused by some structure issues not directly next to the error location, so you need to look around for potential errors. For example, you intended to declare a method of an object, but you declared it as a propety instead:
96+
97+
```js-nolint example-bad
98+
const MyComponent = {
99+
mounted: {
100+
document.getElementById("app").classList.add("loaded");
101+
}
102+
}
103+
```
104+
105+
The `.` after `document` is unexpected, because JavaScript is parsing the `{}` as an object literal instead of a function body, so it expects a `:`. The problem is solved by declaring `mounted` as function.
106+
107+
```js-nolint example-good
108+
const MyComponent = {
109+
mounted() {
110+
document.getElementById("app").classList.add("loaded");
111+
}
112+
}
113+
```
114+
93115
## See also
94116

95117
- {{jsxref("SyntaxError")}}

0 commit comments

Comments
 (0)