-
Notifications
You must be signed in to change notification settings - Fork 22.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mismatched placement JavaScript error (#28022)
* Add mismatched placement JavaScript error https://bugzilla.mozilla.org/show_bug.cgi?id=1707974 * Move file; style changes --------- Co-authored-by: Joshua Chen <[email protected]>
- Loading branch information
1 parent
aa8d411
commit 18f6260
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...us/web/javascript/reference/errors/either_be_both_static_or_non-static/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
title: "SyntaxError: getter and setter for private name #x should either be both static or non-static" | ||
slug: Web/JavaScript/Reference/Errors/Either_be_both_static_or_non-static | ||
page-type: javascript-error | ||
--- | ||
|
||
{{jsSidebar("Errors")}} | ||
|
||
The JavaScript exception "mismatched placement" occurs when a private [getter](/en-US/docs/Web/JavaScript/Reference/Functions/get) and [setter](/en-US/docs/Web/JavaScript/Reference/Functions/set) are mismatched in whether or not they are {{jsxref("Classes/static", "static")}}. | ||
|
||
## Message | ||
|
||
``` | ||
SyntaxError: Identifier '#x' has already been declared (V8-based) | ||
SyntaxError: getter and setter for private name #x should either be both static or non-static (Firefox) | ||
SyntaxError: Cannot declare a private non-static getter if there is a static private setter with used name. (Safari) | ||
``` | ||
|
||
## Error type | ||
|
||
{{jsxref("SyntaxError")}} | ||
|
||
## What went wrong? | ||
|
||
Private [getters](/en-US/docs/Web/JavaScript/Reference/Functions/get) and [setters](/en-US/docs/Web/JavaScript/Reference/Functions/set) for the same name must either be both {{jsxref("Classes/static", "static")}}, or both non-static. This limitation does not exist for public methods. | ||
|
||
## Examples | ||
|
||
### Mismatched placement | ||
|
||
```js example-bad | ||
class Test { | ||
static set #foo(_) {} | ||
get #foo() {} | ||
} | ||
|
||
// SyntaxError: getter and setter for private name #foo should either be both static or non-static | ||
``` | ||
|
||
Since `foo` is [private](/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields), the methods must be either both {{jsxref("Classes/static", "static")}}: | ||
|
||
```js example-good | ||
class Test { | ||
static set #foo(_) {} | ||
static get #foo() {} | ||
} | ||
``` | ||
|
||
or non-static: | ||
|
||
```js example-good | ||
class Test { | ||
set #foo(_) {} | ||
get #foo() {} | ||
} | ||
``` | ||
|
||
## See also | ||
|
||
- {{jsxref("Functions/get", "get")}} | ||
- {{jsxref("Functions/set", "set")}} | ||
- {{jsxref("Classes/static", "static")}} | ||
- [Private class features](/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields) |