Continuing woes in reading the ES6 spec language

Tab Atkins Jr. jackalmage at gmail.com
Fri Sep 13 15:10:13 PDT 2013


On Fri, Sep 13, 2013 at 3:06 PM, Axel Rauschmayer <axel at rauschma.de> wrote:
>>> * You can have default parameters before parameters without defaults.
>>
>> The spec explicitly states that any parameter without a default, but after
>> another parameter that does have a default assignment implicitly has a
>> default property of undefined
>
> As an aside: This can be useful. For example:
>
> ```js
> function getRandomInteger(lower = 0, upper) {
>     return Math.floor(Math.random() * (upper - lower)) + lower;
> }
> ```
>
> Implementing this function in some other manner is much more complicated.

This doesn't do what you appear to think it does.  In particular,
calling it with "getRandomInteger(5)" is *not* equivalent to calling
it with "getRandomInteger(0, 5)", it's equivalent to calling it with
"getRandomInteger(5, undefined)".

This is precisely what the spec section you've quoted says, so I'm not
sure how you're confused on this point.

~TJ


More information about the es-discuss mailing list