Destructuring and default parameters in functions

# Marius Gundersen (12 years ago)

I'm testing some code in the latest version of Firefox, and it seems I'm not able to define default values to function arguments which are destructured. For example:

function test([a, b] = [1, 2]){

    return a+b;
}


test([3, 4])//expect 7

test([3])//expect NaN

test()//expect 3

//Exception: missing ) after formal parameters (line 1)

I haven't found this defined in the current grammar, but would be useful for something I'm currently working on. Or maybe it just isn't implemented in Firefox?

# Erik Arvidsson (12 years ago)

That should work and it works both in Traceur and Continuum.

# Allen Wirfs-Brock (12 years ago)

On Jun 27, 2013, at 3:19 AM, Marius Gundersen wrote:

I haven't found this defined in the current grammar,

see people.mozilla.org/~jorendorff/es6-draft.html#sec-13

FormalParameter :
    BindingElement

people.mozilla.org/~jorendorff/es6-draft.html#sec-12.2.4

BindingElement :
    BindingPattern Initialiser<opt>

    BindingPattern :
        ArrayBindingPattern
# Brandon Benvie (12 years ago)

On 6/27/2013 3:19 AM, Marius Gundersen wrote:

Or maybe it just isn't implemented in Firefox?

This is correct. bugzilla.mozilla.org/show_bug.cgi?id=884372

# Marius Gundersen (12 years ago)

Good to see that the expected functionality is also how it is intended to function. Hopefully the bug Brandon referred to will be fixed soon, so it can be tested in a browser.

By using default params and destructuring a neat module loading syntax can be defined (as opposed to the import syntax of ES6 or the array in AMD): gist.github.com/mariusGundersen/5884450

Marius Gundersen