Destructuring instances with private fields

kdex kdex at kdex.de
Wed Sep 5 20:11:31 UTC 2018


Often times, when I use a class field a lot, I create a local binding for it, 
so that I don't have to prefix every appearance with `this.`:

```js
class Foo {
	bar = 1;
	method() {
		const { bar } = this;
		/* … */
	}
}
```

The same approach would currently be illegal syntax if `bar` is declared to be 
private:

```js
class Foo {
	#bar = 1;
	method() {
		const { #bar } = this;
		/* … */
	}
}
```

How is the destructuring assignment supposed to behave anyway? Introduce a 
local `bar` binding, just like above?

Whatever it should do, even defining an explicit name for the binding is 
currently illegal:

```js
class Foo {
	#bar = 1;
	method() {
		const { #bar: bar } = this;
		/* … */
	}
}
```

This feels really asymmetric to public fields and has come up in the class 
fields proposal[1] before, though it was suggested to defer it to a separate 
proposal.

Is someone currently working on said proposal?

[1] https://github.com/tc39/proposal-class-fields/issues/4
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180905/5f3f91b4/attachment.sig>


More information about the es-discuss mailing list