I am having trouble passing arguments through a new object when using an initialiser function. Consider my following examples that I want to be able to create an object that returns an array. Object Ex1 does this fine:
Ex1 = function() {
myVar = [];
myVar = Array.apply( myVar, arguments );
return myVar;
};
ex1 = new Ex1( 'red', 'green', 'blue' );
console.log( ex1);
/* ["red", "green", "blue"] */
However, I want to us an initialiser to keep my code clean. Object Ex2 shows my unexpected result:
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"
}] */
As you can see from the log the result is not a clean array.
How do I return an array when creating a new object when using an initialiser function and passing arguments to it?