-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add docs for no-private-class-fields
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
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,42 @@ | ||
# no-private-class-fields | ||
|
||
This prevents the use of Private Class Fields | ||
|
||
```js | ||
class Foo { | ||
static #bar = 1 | ||
#bar = 1 | ||
|
||
isFoo() { | ||
return #bar === 1 | ||
} | ||
} | ||
``` | ||
|
||
These will not be allowed because they are not supported in the following browsers: | ||
|
||
- Edge (any version at the time of writing) | ||
- Safari (any version at the time of writing) | ||
- Firefox (any version at the time of writing) | ||
- Chrome < 74 | ||
|
||
|
||
## What is the Fix? | ||
|
||
Use of a WeakMap will cover most use cases: | ||
|
||
```js | ||
const fooPrivateState = new WeakMap() | ||
|
||
class Foo { | ||
constructor() { | ||
fooPrivateState.set(this, { bar: 1 }) | ||
} | ||
|
||
isFoo() { | ||
return (fooPrivateState.get(this) || {}).bar === 1 | ||
} | ||
} | ||
``` | ||
|
||
This can be safely disabled if you intend to compile code with the `@babel/plugin-proposal-class-properties` Babel plugin. |