I'm using the following pattern for my class constructor:
function SomeConstructor(a){
if (!(this instanceof arguments.callee)){
return new arguments.callee(a);
}
//the constructor properties and methods here
}
This works well when I know the number of arguments I'm passing to the constructor. The problem is that in some cases I do not, when using inheritance, for example:
SomeConstructor.prototype = AnotherClass();
This is not as simple as looking up the definition of AnotherClass, since this logic is part of a framework and gets generated automatically. I tried to work around this by calling apply
method on the arguments.callee
, but receive the following error:
function apply() { [native code] } is not a constructor
Passing arguments to callee directly won't work, since it's an array. Is there something similar to apply
that works with constructors or a workaround that I'm not seeing here?
Also, this is an internal framework, so I can tweak what the constructor generates, the only requirements are:
- we must be able to instantiate classes without the use of
new
keyword from outside - classes have to be inheritable such that the child constructor may not be aware (at compile time) of all the arguments that the parent receives
- class initialization has to be in
Class(a, b, c)
format, I can't wrap the arguments in an array
EDIT: The code I tried for apply sequence looked as follows:
function SomeConstructor(){
if (!(this instanceof arguments.callee)){
return new arguments.callee.apply(arguments);
}
//the constructor properties and methods here
}