2

Possible Duplicate:
Use of .apply() with 'new' operator. Is this possible?

I need to create a new insance of a class (new SomeClass()), but the arguments I need to pass is an array. I know I can call functions with apply() and pass the array of arguments as the second argument to apply, but how can I do this when creating a new instance?

Community
  • 1
  • 1
LordZardeck
  • 7,953
  • 19
  • 62
  • 119
  • @JamesMontagne i can't get that example to work though. I get this error: TypeError: Function.prototype.apply: Arguments list has wrong type – LordZardeck Apr 12 '12 at 18:54
  • @LordZardeck: Can you show us the code you're trying. Are you sure you're passing an array? – gen_Eric Apr 12 '12 at 19:10

1 Answers1

0
function ​myClass​()
{
    if(this instanceof arguments.callee)
    {
        init.apply(this, arguments);
    }
    function init()
    {
        this.args=arguments;
        console.log(arguments);
    }
}

var myArray=[1,2,3];
var obj=new myClass(myArray);
console.log(obj.args);

Example here. Hope I understood what you need and this one may help you.

The Alpha
  • 143,660
  • 29
  • 287
  • 307