Get currently destructured object

Michał Wadas michalwadas at gmail.com
Fri May 20 15:11:18 UTC 2016


Yes, in such simple case you save only few keystrokes.

Imagine more complicated example:

function initServer(config) {
    const {env, database, cache, logger} = config;
    const {isDev} = env;
    implementLogger(global, logger, isDev);
    return Promise.all([databaseProvider(database), cacheProvider(cache),
envManager(env)]).then(([database, cache, env])=>({database, cache}));
}

It can be replaced by simpler:

function initServer({env: {isDev, *: env}, database, cache, logger, *:
config}) {
    implementLogger(global, logger, isDev);
    globalLogger.log(config);
    return Promise.all([databaseProvider(database), cacheProvider(cache,
isDev), envManager(env)]).then(([database, cache, env])=>({database, cache,
env}));
}

When destructuring nested objects you can potentially save one line per
nest.



On Thu, May 19, 2016 at 9:47 PM, Isiah Meadows <isiahmeadows at gmail.com>
wrote:

> How is it any different than this, to borrow your first example:
>
> ```js
> function foo(baz) {
>     const {bar} = baz
>     // do things
> }
> ```
>
> On Thu, May 19, 2016, 11:59 Michał Wadas <michalwadas at gmail.com> wrote:
>
>> Idea:
>>
>> function foo({bar, *: baz}) {
>>
>> }
>>
>> would be equivalent to:
>>
>> function foo(baz) {
>>    const bar =  baz.bar;
>> }
>>
>> Rationales:
>> - little better IDE support (argument name reveals intention)
>> - allows to write more concise code when handling case of getting common
>> values and handling whole object optionally
>> - improve pattern "use few properties of options object then pass it
>> deeper"
>> - allows more expressive module import
>> - allows to more concise code when destructuring expression result
>>
>> import {*: rand, generateRandomInt} from 'fancy-random-module';
>> import {*: moment, isMoment} from 'moment';
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160520/18e2aebd/attachment.html>


More information about the es-discuss mailing list