I'm trying to create an Object constructor that receives an arbitrary number of arguments.
It would be convenient to apply the values in an array to the constructor, just as convenient as the 'apply()' method is for functions, but if I try this:
var arr = [1,2,3];
var a = new Obj.apply(null, arr);
It throws:TypeError: function apply() { [native code] } is not a constructor
It would also be convenient so I could make use of the 'new' keyword optional, like:
function Obj() {
if (!(this instanceof)) {
return new Obj.apply(arguments);
}
this.a = arguments[0];
this.b = arguments[1];
this.allArr = Array.prototype.slice.call(arguments);
}
Is there any way around it?