最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - passing unknown count of parameters to js constructor function - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Jan 13, 2012 at 9:35 Ph0en1x asked Dec 5, 2011 at 9:37 Ph0en1xPh0en1x 10.1k8 gold badges55 silver badges97 bronze badges 5
  • What javascript lib are you using? – Stefan Koenen Commented Dec 5, 2011 at 9:39
  • possible duplicate of Javascript unknown number of arguments – Felix Kling 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? – nnnnnn 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 – Ph0en1x Commented Dec 5, 2011 at 10:39
  • duplicate: stackoverflow./questions/3871731/… – Gabriel Llamas Commented Feb 2, 2012 at 18:39
Add a ment  | 

4 Answers 4

Reset to default 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();
}

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);

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

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

发布评论

评论列表(0)

  1. 暂无评论