Extensible destructuring proposal

Claude Pache claude.pache at gmail.com
Tue Jul 21 10:42:40 UTC 2015


> Le 21 juil. 2015 à 08:14, Samuel Hapák <samuel.hapak at vacuumapps.com> a écrit :
> 
> Hello there,
> 
> I have written proposal to allow extend standard behavior of object destructuring:
> https://github.com/vacuumlabs/es-proposals/blob/master/extensible-destructuring.md
> 
> Main idea is, that currently object destructuring is useless for people who use Map or Immutable.Map (https://facebook.github.io/immutable-js/) as their main data structure.
> 
> This would allow authors of libraries to create map like structures that would support destructuring. It extends the language in a similar way iterator does.
> 
> The main idea is, that if object defines `Symbol.get` method, it gets used to access properties of object instead of `[]` when destructuring.
> 
> What do you think?
> 
> Cheers,
> 
> Samuel
> 

An alternative to `Symbol.get` is a Proxy with a "get" trap. Maybe worth experimenting with Proxies?

For example, one can imagine something like:

```js
    const {author: {name: {first, last}, birthdate}} = book.toObject()
```

where `.toObject()` is a method returning a proxy, or, in implementations lacking proxies, falling back to convert eagerly the structure into a plain object.

—Claude



More information about the es-discuss mailing list