destructuring: as patterns?

Claus Reinke claus.reinke at
Wed Apr 18 07:59:28 PDT 2012

Looking through the destructuring proposal

there seems to be no mention of 'as' patterns. In typical pattern
matching constructs (SML, Haskell, ..), 'as' patterns allow to name 
a sub-object while continuing the match for its sub-structures. 

For instance, with 

    var obj = { a: 0, b: { x: 1, y: 2} };

something like

    let { b: b as {x,y} } = obj

would result in the bindings of b to obj.b, x to obj.b.x, y to obj.b.y.

This avoids needless repetition when both a subobject and its
components need to be extracted. Without 'as', each such case
leads to a separate destructuring assignment

    let { b  } = obj
    let { b: {x,y} } = obj

Shouldn't 'as' patterns be included in destructuring? Or have I
missed an equivalent feature?


More information about the es-discuss mailing list