Function.length and Default Parameters

Kevin Smith khs4473 at gmail.com
Wed Oct 10 10:22:32 PDT 2012


>
> We've discussed this extensively before and there doesn't seem to be many
> plausible use cases for the function length property.


Here's the only use case that I've encountered (admittedly not particularly
strong):  Overriding the behavior of a function/method based on the
"signature" of an input function:

    function withCallback(callback) {

      // Do something which generates an error...
      var error = new Error("abc");

      // Use callback.length to override behavior...
      if (callback.length >= 2)
        callback(null, error);
      else
        throw error;
    }

    // Throws error
    withCallback(val => { ... });

    // Logs error
    withCallback((val, err) => { if (err) console.log(err); });

    // Throws, even though it has the err parameter
    withCallback((val = 123, err) => { ... });

Not great, but...

Another thought I had was that someone might want to use defaults to
replace undefined actual parameters with null values:

    function f(a = null, b = null) { ... }
    f(void 0); // normalized to null
    f.length = 0;  // Intentional?

Again, not great, but...

Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121010/ee6bcf5d/attachment.html>


More information about the es-discuss mailing list