Enforcing arity?

Rick Waldron waldron.rick at gmail.com
Sun Jan 6 18:26:34 PST 2013


On Sunday, January 6, 2013, Axel Rauschmayer wrote:

> What is the simplest way of enforcing an arity in ES6? Doesn’t it involve
> arguments?
>
> function add(x, y) {
>     if (arguments.length !== 2) throw ...
> }
>

> To avoid `argument`, one could:
> - ensure a maximum arity by adding a ...rest parameter and checking that
> its length is 0.
>

I can't find the thread, but a similar question was asked sometime in the
last 6 months (I think??) and this was the answer that was ultimately
agreed on as the best way to check arity length.


- ensure a minimum arity, by giving y a default value and checking for it.
>

Explicit undefined arguments, intentional or not, would trigger the default
value.

Rick


>
> Would fail-fast destructuring work?
>
> function add(...args) {
>     let [x,y] = args;
> }
>
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de <javascript:_e({}, 'cvml', '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/20130106/49ebcce7/attachment.html>


More information about the es-discuss mailing list