return when desugaring to closures

Brendan Eich brendan at mozilla.org
Thu Aug 21 18:34:06 PDT 2008


On Aug 21, 2008, at 6:19 PM, Mark S. Miller wrote:

> I am in favor of all these including "destructuring binding forms".
> But I don't know what "destructuring assignment" is.

Same thing without the binding keyword (let, var, const):

js> function foo() { return [1,2,3] }
js> let [a,b,c] = foo()
js> print(a,b,c)
1 2 3
function print() {
     [native code]
}
js> [d,e,f] = foo()
1,2,3

The destructuring pattern here is an array, but objects work too:

js> let {length: len} = foo()
js> len
3

The assignment version of the above requires parentheses, to avoid  
the { being taken for start of block statement:

js> ({length:len} = foo())
1,2,3

You could parenthesize just the left-hand side, of course.

There's a shorthand, familiar to SML users, for the common case where  
you want the property name and the binding name to be the same:

js> function bar() { return {p:42, q:"hi"} }
js> let {p, q} = bar()
js> p
42
js> q
hi

The destructuring patterns work in formal parameter position and  
catch clauses too. They nest too, so you can extract deep property  
values and bind them to variable names.

Our experience in JS1.7 and 1.8 is that destructuring is extremely  
pleasant and popular, with no nasty surprises (not even the  
parenthesization requirement for object patterns in destructuring  
assignment).

/be
>



More information about the Es-discuss mailing list