-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing docs for proposal-private-methods (#1967)
Fixes #1966.
- Loading branch information
1 parent
421d691
commit 335b376
Showing
6 changed files
with
730 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,141 @@ | ||
--- | ||
id: babel-plugin-proposal-private-methods | ||
title: @babel/plugin-proposal-private-methods | ||
sidebar_label: proposal-private-methods | ||
--- | ||
|
||
## Example | ||
|
||
```js | ||
class Counter extends HTMLElement { | ||
#xValue = 0; | ||
|
||
get #x() { return this.#xValue; } | ||
set #x(value) { | ||
this.#xValue = value; | ||
window.requestAnimationFrame( | ||
this.#render.bind(this)); | ||
} | ||
|
||
#clicked() { | ||
this.#x++; | ||
} | ||
} | ||
``` | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install @babel/plugin-proposal-private-methods | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (Recommended) | ||
|
||
**.babelrc** | ||
|
||
Without options: | ||
|
||
```json | ||
{ | ||
"plugins": ["@babel/plugin-proposal-private-methods"] | ||
} | ||
``` | ||
|
||
With options: | ||
|
||
```json | ||
{ | ||
"plugins": [["@babel/plugin-proposal-private-methods", { "loose": true }]] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
$ babel --plugins @babel/plugin-proposal-private-methods script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("@babel/core").transform("code", { | ||
plugins: ["@babel/plugin-proposal-private-methods"], | ||
}); | ||
``` | ||
|
||
## Options | ||
|
||
### `loose` | ||
|
||
`boolean`, defaults to `false`. | ||
|
||
> Note: The `loose` mode configuration setting _must_ be the same as [`@babel/proposal-class-properties`](plugin-proposal-class-properties.md). | ||
When true, private methods will be assigned directly on its parent | ||
via `Object.defineProperty` rather than a `WeakSet`. This results in improved | ||
performance and debugging (normal property access vs `.get()`) at the expense | ||
of potentially leaking "privates" via things like `Object.getOwnPropertyNames`. | ||
|
||
Let's use the following as an example: | ||
|
||
```javascript | ||
class Foo { | ||
constructor() { | ||
this.publicField = this.#privateMethod(); | ||
} | ||
|
||
#privateMethod() { | ||
return 42; | ||
} | ||
} | ||
``` | ||
|
||
By default, this becomes: | ||
|
||
```javascript | ||
var Foo = function Foo() { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
|
||
_privateMethod.add(this); | ||
|
||
this.publicField = babelHelpers | ||
.classPrivateMethodGet(this, _privateMethod, _privateMethod2) | ||
.call(this); | ||
}; | ||
|
||
var _privateMethod = new WeakSet(); | ||
|
||
var _privateMethod2 = function _privateMethod2() { | ||
return 42; | ||
}; | ||
``` | ||
|
||
With `{ loose: true }`, it becomes: | ||
|
||
```javascript | ||
var Foo = function Foo() { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
Object.defineProperty(this, _privateMethod, { | ||
value: _privateMethod2, | ||
}); | ||
this.publicField = babelHelpers | ||
.classPrivateFieldLooseBase(this, _privateMethod) | ||
[_privateMethod](); | ||
}; | ||
|
||
var _privateMethod = babelHelpers.classPrivateFieldLooseKey("privateMethod"); | ||
|
||
var _privateMethod2 = function _privateMethod2() { | ||
return 42; | ||
}; | ||
``` | ||
|
||
## References | ||
|
||
- [Proposal: Private methods and getter/setters for JavaScript classes](https://github.com/tc39/proposal-private-methods) |
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
137 changes: 137 additions & 0 deletions
137
website/versioned_docs/version-7.2.0/plugin-proposal-private-methods.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,137 @@ | ||
--- | ||
id: version-7.2.0-babel-plugin-proposal-private-methods | ||
title: @babel/plugin-proposal-private-methods | ||
sidebar_label: proposal-private-methods | ||
original_id: babel-plugin-proposal-private-methods | ||
--- | ||
|
||
> Note: Support for private accessors (getters and setters) was added in [7.3.0](https://babeljs.io/blog/2019/01/21/7.3.0)! | ||
## Example | ||
|
||
```js | ||
class Counter extends HTMLElement { | ||
#xValue = 0; | ||
|
||
#clicked() { | ||
this.#x++; | ||
} | ||
} | ||
``` | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install @babel/plugin-proposal-private-methods | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (Recommended) | ||
|
||
**.babelrc** | ||
|
||
Without options: | ||
|
||
```json | ||
{ | ||
"plugins": ["@babel/plugin-proposal-private-methods"] | ||
} | ||
``` | ||
|
||
With options: | ||
|
||
```json | ||
{ | ||
"plugins": [["@babel/plugin-proposal-private-methods", { "loose": true }]] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
$ babel --plugins @babel/plugin-proposal-private-methods script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("@babel/core").transform("code", { | ||
plugins: ["@babel/plugin-proposal-private-methods"], | ||
}); | ||
``` | ||
|
||
## Options | ||
|
||
### `loose` | ||
|
||
`boolean`, defaults to `false`. | ||
|
||
> Note: The `loose` mode configuration setting _must_ be the same as [`@babel/proposal-class-properties`](plugin-proposal-class-properties.md). | ||
When true, private methods will be assigned directly on its parent | ||
via `Object.defineProperty` rather than a `WeakSet`. This results in improved | ||
performance and debugging (normal property access vs `.get()`) at the expense | ||
of potentially leaking "privates" via things like `Object.getOwnPropertyNames`. | ||
|
||
Let's use the following as an example: | ||
|
||
```javascript | ||
class Foo { | ||
constructor() { | ||
this.publicField = this.#privateMethod(); | ||
} | ||
|
||
#privateMethod() { | ||
return 42; | ||
} | ||
} | ||
``` | ||
|
||
By default, this becomes: | ||
|
||
```javascript | ||
var Foo = function Foo() { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
|
||
_privateMethod.add(this); | ||
|
||
this.publicField = babelHelpers | ||
.classPrivateMethodGet(this, _privateMethod, _privateMethod2) | ||
.call(this); | ||
}; | ||
|
||
var _privateMethod = new WeakSet(); | ||
|
||
var _privateMethod2 = function _privateMethod2() { | ||
return 42; | ||
}; | ||
``` | ||
|
||
With `{ loose: true }`, it becomes: | ||
|
||
```javascript | ||
var Foo = function Foo() { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
Object.defineProperty(this, _privateMethod, { | ||
value: _privateMethod2, | ||
}); | ||
this.publicField = babelHelpers | ||
.classPrivateFieldLooseBase(this, _privateMethod) | ||
[_privateMethod](); | ||
}; | ||
|
||
var _privateMethod = babelHelpers.classPrivateFieldLooseKey("privateMethod"); | ||
|
||
var _privateMethod2 = function _privateMethod2() { | ||
return 42; | ||
}; | ||
``` | ||
|
||
## References | ||
|
||
- [Proposal: Private methods and getter/setters for JavaScript classes](https://github.com/tc39/proposal-private-methods) |
Oops, something went wrong.