1

I use some javascript library for drawing charts. It need to pass arrays with coordinates in to chart initializer. But chart initializer function have a signature looks like new function ([arr1], [arr2], [arr3]), but I don't know how much arrays I will pass into that, but it impossible to initialize it another way. How it possible to solve this? Thanks.

UPD: function called is constructor, so it's not work correctly with apply. Problem is how to pass data, but not how to get the count of passed data

Ph0en1x
  • 9,943
  • 8
  • 48
  • 97
  • What javascript lib are you using? – Stefan Koenen Dec 05 '11 at 09:39
  • possible duplicate of [Javascript unknown number of arguments](http://stackoverflow.com/questions/6442371/javascript-unknown-number-of-arguments) – Felix Kling Dec 05 '11 at 09:44
  • I'm having trouble understanding what you're asking (even with the update). Are you saying the initialiser function is part of the library so you don't control the way it works, that it expects three parameters that are arrays, and you don't know what to pass it? Can you make it clearer what data you _do_ have? What happens if you pass empty arrays or null in place of parameters you don't have values for? – nnnnnn Dec 05 '11 at 10:13
  • initializer function expected any count of parameters, but parameter should be passed into this function only in specific notation – Ph0en1x Dec 05 '11 at 10:39
  • duplicate: http://stackoverflow.com/questions/3871731/dynamic-object-construction-in-javascript – Gabriel Llamas Feb 02 '12 at 18:39

4 Answers4

5

SOLUTION: It's possible to use apply in constructor, but it needs the following workaround:

function construct(constructor, args) {
    function F() {
        return constructor.apply(this, args);
    }
    F.prototype = constructor.prototype;
    return new F();
}
Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
Ph0en1x
  • 9,943
  • 8
  • 48
  • 97
3

You can use the built in variable called arguments which is an array-like object of the arguments passed to the function and arguments.length to determine how many arguments were passed. See this MDN reference for more info.

function myFunc() {
    for (var i = 0; i < arguments.length; i++) {
        // you can process arguments[i] here
    }
}

If you're trying to pass a variable number of arguments, you can use .apply(). You construct an array of the arguments and pass that as the second argument to apply. The first argument is what you want the this pointer to be set to. Here's the MDN reference for .apply().

var myArgs = [arr1, arr2, arr3];
myFunc.apply(window, myArgs);
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

You want to use the arguments object. It's basically an array-like object with all your arguments in it. You can use it like this:

function yourFunction() {
    for (var i = 0, len = arguments.length; i < len; i++) {
        console.log(arguments[i]); //That's the argument you passed in.
    }
}

Demo

Some Guy
  • 15,854
  • 10
  • 58
  • 67
0

Functions that accept a variable number of arguments are called variadic functions. In JavaScript you can use the arguments object to access the arguments. To invoke the function you can also use .call or .apply

alex
  • 479,566
  • 201
  • 878
  • 984
Nenad
  • 3,438
  • 3
  • 28
  • 36