Background

Goals

  • Provide a better arguments so we can deprecate, obsolete, and some years hence burn with fire / salt the earth anything to do with arguments, foo.arguments, etc.
  • Capture trailing arguments after one or more fixed formal parameters

Sketch

  • Allow ... as an optional prefix before the last formal parameter in a function declaration
  • Presence of ... in function foo(...rest) {...} creates a rest parameter, rest, denoting a new Array created when foo is called, whose elements from 0 to foo.length are supplied by the actual arguments passed to foo (if any)
  • Similarly for function bar(baz, bletch, ...rest) {...}, with elements of rest being supplied by the third through last actual parameters
// Trivial this-free wrapper around Function.prototype.apply:
function simpleApply(func, ...args) {
    "use strict";
    return func.apply(undefined, args);
}

// Simple printf-style string formatter, write and convert left as exercises:
function printf(format, ...args) {
    var n = 0;
    var i = 0, j;

    while ((j = format.indexOf('%', i)) >= 0) {
        if (n >= args.length)
            throw "printf: too few args for given format " + format;
        write(format.slice(i, j));

        // convert takes exactly one format-specifier char, to keep things simple.
        write(convert(s[j + 1], args[n++]));
        i = j + 2;
    }
    write(format.slice(i));
}

Issues

  • Is there any benefit to allowing ... with no formal parameter identifier after it? Not without types or arity checks.
  • Should rest parameters be frozen? There is no aliasing hazard, so freezing is premature and limits utility, IMHO.

Brendan Eich 2009/05/04 22:52

 
harmony/rest_parameters.txt · Last modified: 2009/05/06 02:17 by brendan
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki