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!

Community
  • 1
  • 1
gztomas
  • 3,030
  • 3
  • 27
  • 38
  • 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])` – Lee Kowalkowski Mar 22 '12 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]) – Bilthon Jun 04 '14 at 04:04

0 Answers0