Apply array to constructor function in Javascript?

Possible duplicate:
Using .apply () with the 'new' operator. Is it possible?
How to build a JavaScript object (using "apply")?

Say I have a function like this:

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

I understand that this is equivalent to calling it in two ways:

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

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

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

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

I believe that I can’t change the definition of Foo, and I don’t want to explicitly extract the parameters a, b, c, d from [a, b, c, d]

Thanks!

+6
source share

Source: https://habr.com/ru/post/911448/


All Articles