Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6534921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:18:34+00:00 2026-05-25T10:18:34+00:00

The length property of functions tells how long the ‘expected’ argument list is: console.log((function

  • 0

The length property of functions tells how long the ‘expected’ argument list is:

console.log((function () {}).length);  /* 0 */
console.log((function (a) {}).length); /* 1 */
console.log((function (a, b) {}).length); /* 2 etc. */

However, it is a readonly method:

f = function (a) {};
alert(f.length); // 1
f.length = 3;
alert(f.length); // 1

Is there a way to programmatically set that length? The closest I’ve come so far is to use the Function constructor:

f = new Function("a,b,c", "/* function body here */");
f.length; // 3

However, using Function is essentially the same as eval and we all know how bad that is. What other options do I have here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:18:35+00:00Added an answer on May 25, 2026 at 10:18 am

    For now, here’s the best solution I could think of.

    makeFunc = function (length, fn) {
        switch (length) {
        case 0 : return function () { return fn.apply(this, arguments); };
        case 1 : return function (a) { return fn.apply(this, arguments); };
        case 2 : return function (a,b) { return fn.apply(this, arguments); };
        case 3 : return function (a,b,c) { return fn.apply(this, arguments); };
        case 4 : return function (a,b,c,d) { return fn.apply(this, arguments); };
        case 5 : return function (a,b,c,d,e) { return fn.apply(this, arguments); };
        case 6 : return function (a,b,c,d,e,f) { return fn.apply(this, arguments); };
        case 7 : return function (a,b,c,d,e,f,g) { return fn.apply(this, arguments); };
        case 8 : return function (a,b,c,d,e,f,g,h) { return fn.apply(this, arguments); };
        case 9 : return function (a,b,c,d,e,f,g,h,i) { return fn.apply(this, arguments); };
        default : return function (a,b,c,d,e,f,g,h,i,j) { return fn.apply(this, arguments); };
        }
    };
    

    Example usage:

    var realFn = function () {
        return "blah";
    };
    
    lengthSix = makeFunc(6, realFn);
    
    lengthSix.length; // 6
    lengthSix(); // "blah"
    

    Personally, I always cringe whenever I use copy and paste when programming, so I’d be very happy to hear of any better options.

    Update

    I thought of a method which could work for any arbitrary size, unlike the example above which is limited by how many times you want to copy-and-paste. Essentially, it dynamically creates a function (using new Function) which will return a function of the right size which then just proxies through to whatever function you pass to it. Yeah that does hurt your head. Anyway, I thought I’d benchmark it against the above…

    http://jsperf.com/functions-with-custom-length (you can see the ‘evil’ code there too).

    The evil method is many hundreds of times slower than the hacky copypasta method, so there you go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find out if length property for Java arrays is an int/long
I´m testing the length property which returns this error: $(div.rows).children().length is not a function
I need a function that gets two array parameters of the same length: property-names
I have a function which returns a list of property values from a collection:
I have this part of code in my application. card.getcard(command, function(toproceed,resultscard) { console.log('entry other
In C# I use the Length property embedded to the array I'd like to
I get the Total length of columns in constraint is too long. erro from
I have the following function whose last argument is a name of the text
If the arguments is just an object with a length property, then why does
I want to check in my function if a passed argument of type object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.