Skip to content

Commit

Permalink
Add another cause for unexpected token error (#36624)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
kingma-sbw and Josh-Cena authored Nov 5, 2024
1 parent 93a294e commit b903e95
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ function round(n, upperBound, lowerBound) {
}
```

### A structure error further up confused the meaning

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:

```js-nolint example-bad
const MyComponent = {
mounted: {
document.getElementById("app").classList.add("loaded");
}
}
```

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.

```js-nolint example-good
const MyComponent = {
mounted() {
document.getElementById("app").classList.add("loaded");
}
}
```

## See also

- {{jsxref("SyntaxError")}}

0 comments on commit b903e95

Please sign in to comment.