6

Possible Duplicate:
Use of .apply() with 'new' operator. Is this possible?
How to construct JavaScript object (using 'apply')?

Let's say I have a function like this one:

var Foo = function(param1, param2, param3, param4) {
...
}

I understand it is equivalent to call it in these two ways:

Foo(a, b, c, d)
Foo.apply(this, [a, b, c, d])

Let's say now I use the function to create an object:

var myObject = new Foo(a, b, c, d);

If I already have the arguments in an array [a, b, c, d], how can I call the function with the new operator an also pass the parameters with the array, like I did with apply above?

Consider I cannot modify the definition of Foo, and I don't wan't to explicitly extract the parameters a, b, c, d from [a, b, c, d]

Thanks!

2
  • 2
    The duplicates are not 'exact' (they're overcomplicated whereas your question is simple), basically, you just need to use your new object instead of 'this' when you call apply: var myObject = {}; Foo.apply(myObject, [a, b, c, d]) Commented Mar 22, 2012 at 21:29
  • What is wrong with the 'Foo.apply(this, [a, b, c, d])'. I just ran into this issue and simply dropped the 'new' in favor of doing something like: var myfoo = Foo.apply(this, [a, b, c, d]) Commented Jun 4, 2014 at 4:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.