Skip to content

Commit

Permalink
docs: add docs for no-private-class-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Sep 20, 2019
1 parent f3b73ef commit 67db885
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/no-private-class-fields.md
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.

0 comments on commit 67db885

Please sign in to comment.