destructuring: as patterns?

David Herman dherman at mozilla.com
Thu Apr 19 16:16:19 PDT 2012


On Apr 19, 2012, at 2:18 PM, Allen Wirfs-Brock wrote:

> //initialize some variable with default objects
> let {
>     unidentifedAdult: mom,
>     unidetifiedAdult: dad, 
>     unidentiedChild: brother,
>     unidentifiedChild: sister
>     } = peopleConstants;
> 
> why is this less desirable than:
> 
> //initialize some variable with default objects
> let mon = peopleConstants.unidentifiedAdult,
>      dad = peopleConstants.unidentifiedAdult,
>      brother = peopleConstants.unidentifiedChild,
>      sister = peopleConstants.unidentifiedChild;

Well, it's rare that you *need* to re-evaluate the dot-pattern. It saves typing, repetition (DRY!), side-effects, and running time to evaluate each selector only once:

    let mom = peopleConstants.unidentifiedAdult,
        dad = mom,
        brother = peopleConstants.unidentifiedChild,
        sister = brother;

And you should be able to do that just fine with nested as-patterns:

    let { unidentifiedAdult: mom as dad, unidentifiedChild: sister as brother }

or

    let { undentifiedAdult as mom as dad, unidentifiedChild as sister as brother }

Dave



More information about the es-discuss mailing list