Patterns in parameter declarations: irrefutable?

Axel Rauschmayer axel at rauschma.de
Wed Mar 20 12:51:51 PDT 2013


Are patterns in parameter declarations irrefutable? In other words, given:

    function foo({ opt1 }) { return opt1 }

What happens if we call foo()?
    1. An exception is thrown
    2. undefined is returned
I’d expect (2). If opt1 had a default value, I’d expect that value to be returned.

What happens if we call foo({})?
    1. An exception is thrown
    2. undefined is returned

I’m asking, because I’ve seen the pattern

    function foo({ opt1 = 3 } = {}) { return opt1 }

If explicitly marking something as irrefutable is necessary, I’d prefer:

    function foo({ opt1 = 3 }?) { return opt1 }


It’s a bit of a shame that destructuring and parameter declarations diverge, but I guess it can’t be helped.

Axel

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130320/37f9604d/attachment.html>


More information about the es-discuss mailing list