I am struggling with passing arguments to a new object when using an initializer function. Let's look at my examples where I aim to create an object that returns an array. Object Ex1 works as expected:
Ex1 = function() {
myVar = [];
myVar = Array.apply( myVar, arguments );
return myVar;
};
ex1 = new Ex1( 'red', 'green', 'blue' );
console.log( ex1);
/* ["red", "green", "blue"] */
However, I prefer using an initialiser for cleaner code. Object Ex2 demonstrates the issue I am facing:
Ex2 = function() {
init = function() {
myVar = [];
myVar = Array.apply( myVar, arguments );
return myVar;
};
return init( arguments );
};
ex2 = new Ex2( 'red', 'green', 'blue' );
console.log( ex2 );
/* [[object Arguments] {
0: "red",
1: "green",
2: "blue"
}] */
The log shows the result is not a neat array.
How can I ensure that I get an array when creating a new object by using an initializer function and passing arguments to it?