0

This is a typical implementation of an asynchronous function I found in a 2016 book. We declare var Obj, then we add a method

Obj.prototype.doSomething(arg1_){}

Then we call it

test.doSomething(number, function(err,value){}

The callback function is accessible via arguments[]

var callback_ = arguments[arguments.length - 1];

So even though the callback function was not defined as an argument in the prototype we can still pass them in? So we can pass as many functions as we want i.e. up to 255?

Here is a pastebin

or

var fib = function (n) {
    if (n < 2) return n;
    return fib(n - 1) + fib(n - 2);
};
var Obj = function() { };
Obj.prototype.doSomething = function(arg1_) {
   var callback_ = arguments[arguments.length - 1];  
   callback = (typeof(callback_) == 'function' ? callback_ : null);
   var arg1 = typeof arg1_ === 'number' ? arg1_ : null;
   if (!arg1) return callback(new Error('first arg missing or not a number'));

   process.nextTick(function() {
     // block on CPU
     var data = fib(arg1);
     callback(null, data);
    });
}
var test = new Obj();
var number = 10;
test.doSomething(number, function(err,value) {
      if (err)
         console.error(err);
      else
         console.log('fibonaci value for %d is %d', number, value);
});
console.log('called doSomething');
Marina Dunst
  • 859
  • 1
  • 6
  • 19
  • you can define `function f(){}` and use it like `f(arg1, arg2, arg3);` – Maxx Oct 18 '16 at 15:44
  • Generally speaking, this is a horrible pattern, you should know what arguments to expect, and what to pass in, not guess how many arguments there are, or that the last one might be a callback etc. Also, today, one could just return a promise instead of the callback, and make the function chainable with `.then()` – adeneo Oct 18 '16 at 15:48
  • The arguments object can handle indefinite number of arguments. Indefinite doesn't mean limitless; depending on your session it might be a value between 150K and 300K. On the other hand unless very necessary it's advisable not to define your prototype methods within the constructor. – Redu Oct 18 '16 at 15:58
  • @Redu - If you have a look at [**MDN**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions) you'll see it repeated ... *"A function can have up to 255 arguments"*, so even if the `arguments` object theoretically could contain more indices, the function constructor generally won't take more than 255 arguments anyway. – adeneo Oct 18 '16 at 16:12
  • 1
    @adeneo Nah.. Don't believe everything you read at MDN. I suppose it's not that frequently updated as the language itself. I just checked though both at Chrome and FF and managed to pass 100K arguments to a constructor function and successfully built an object having them all. – Redu Oct 18 '16 at 16:30
  • I don't know where MDN got that number from, but I suspect that the OP at least got 255 from MDN, since it's telling us that we can have up to 255 arguments. Of course, there's nothing wrong with browsers accepting more arguments, it's not going to break anything, and nobody really uses 255 arguments anyway, so it's probably not an issue. If I remember correctly the `arguments` object is a `List`, which could have billions of values stored, the limitation is in the Function constructor, and it seems to overflow the stack at a couple of hundred K in most browsers, which is a lot more than 255. – adeneo Oct 18 '16 at 16:38
  • @adeneo I'm pretty sure that limit is for *declared parameter names*, not for arguments (i.e. what you really pass). – Bergi Oct 18 '16 at 16:42
  • Another possible duplicate: [Javascript: “Infinite” parameters for function?](http://stackoverflow.com/q/9338291/1048572) – Bergi Oct 18 '16 at 16:43
  • @Bergi . I thought so too, but I've tried declaring, and they take a lot more than 255. I didn't get all the way to 100K, but a few thousand is no problem. – adeneo Oct 18 '16 at 16:46
  • @adeneo then go edit MDN please :-) – Bergi Oct 18 '16 at 16:48
  • @Bergi - I'd have to edit quite a few books and other references as well? – adeneo Oct 18 '16 at 16:50
  • @adeneo Well, quality is what distinguishes MDN from those :-) A wiki always needs community effort. – Bergi Oct 18 '16 at 16:51
  • 1
    @Bergi - I think I know where the 255 limit is from, it used to be the limit in Netscape 5.0, and I think part of one of the early specs, probably before ECMA 3. – adeneo Oct 18 '16 at 16:54

2 Answers2

2

Yes. Its not required to declare all the arguments in javascript. You can retrieve in the arguments array. See some examples here

You can see in this post that the max number of arguments is big enough.

Community
  • 1
  • 1
lucas.coelho
  • 894
  • 1
  • 9
  • 16
0

Javascript doesn't limit you in the number of arguments. You can define a function with one parameter, but call it with 0, 1, 2, or 100 arguments. All of the arguments (and some other stuff) are available using the arguments object, although it's considered a bit difficult to read and doesn't behave exactly like an array in some cases.

GilZ
  • 6,418
  • 5
  • 30
  • 40
  • Actually it does, a function can have up to 255 arguments, no more. – adeneo Oct 18 '16 at 15:56
  • @adeneo If that was the case we wouldn't be able to make `Array.prototype.push.apply(myArray, arrayWithSizeN)` or `myArray.push(...arrayWithSizeN)` for large arrays. – Redu Oct 18 '16 at 16:02
  • @Redu - Arrays and functions aren't the same thing, an array can have approximately 4.29 billion indices according to the specs – adeneo Oct 18 '16 at 16:07
  • @adeneo May be you would like to try this `function test(){console.log(arguments.length);} var a = new Array(100000).fill(); test(...a);` – Redu Oct 18 '16 at 16:17
  • @Redu - Sure, modern browsers can take any number of arguments, it doesn't stop until the stack gets overloaded, doesn't mean it's expected behaviour, the maximum number of arguments is still 255 according to MDN – adeneo Oct 18 '16 at 16:27