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

5
  • What javascript lib are you using? Commented Dec 5, 2011 at 9:39
  • possible duplicate of Javascript unknown number of arguments Commented Dec 5, 2011 at 9: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? Commented Dec 5, 2011 at 10:13
  • initializer function expected any count of parameters, but parameter should be passed into this function only in specific notation Commented Dec 5, 2011 at 10:39
  • duplicate: stackoverflow.com/questions/3871731/… Commented Feb 2, 2012 at 18:39

4 Answers 4

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

in the constructor the this variable will be the F function instance
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);

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.