M
Matt Eberts
Sorry, bad title. Anyway, is there a way to pass the arguments to an object
instantiated via a constructor using the arguments object and have it
expanded, so to speak, so that it doesn't appear as a single argument? I'm
sorry, this explanation is just atrocious, but I can't think of exactly how
to word it. Maybe an example...
Take for instance Function.apply. It takes 1-2 arguments, the first being
the object to use as the context, and the second being either an array or an
instance of the arguments object which are to be the arguments for the
function. I want to do something similar but I want to also basically use
the new operator so that I get back an object.
Here's a snippet of some of my code, maybe this will help:
function Singleton()
{
this.construct.apply(this, arguments);
}
Singleton.extend(JSClass);
Singleton.instance = null;
Singleton.getInstance = function()
{
if(!this.instance)
{
//I want to be able to combine these two lines. If I just do new
this(arguments), then the constructor
//thinks there is only one argument; the arguments object isn't
expanded.
this.instance = new this();
this.instance.construct.apply(this.instance, arguments);
}
return this.instance;
}
function Test()
{
this.construct.apply(this, arguments);
}
Test.extend(Singleton);
Test.getInstance = function()
{
return Test.supers["Singleton"].getInstance.apply(this,
arguments);
};
Test.prototype.name = null;
Test.prototype.construct = function()
{
switch(arguments.length)
{
case 0:
this.name = "Test";
break;
case 1:
this.name = arguments[0];
break;
}
};
Anyway, I hope that despite this rather terrible explanation I've somehow
managed to get my point across. Any help would be much appreciated.
Thanks.
Matt
instantiated via a constructor using the arguments object and have it
expanded, so to speak, so that it doesn't appear as a single argument? I'm
sorry, this explanation is just atrocious, but I can't think of exactly how
to word it. Maybe an example...
Take for instance Function.apply. It takes 1-2 arguments, the first being
the object to use as the context, and the second being either an array or an
instance of the arguments object which are to be the arguments for the
function. I want to do something similar but I want to also basically use
the new operator so that I get back an object.
Here's a snippet of some of my code, maybe this will help:
function Singleton()
{
this.construct.apply(this, arguments);
}
Singleton.extend(JSClass);
Singleton.instance = null;
Singleton.getInstance = function()
{
if(!this.instance)
{
//I want to be able to combine these two lines. If I just do new
this(arguments), then the constructor
//thinks there is only one argument; the arguments object isn't
expanded.
this.instance = new this();
this.instance.construct.apply(this.instance, arguments);
}
return this.instance;
}
function Test()
{
this.construct.apply(this, arguments);
}
Test.extend(Singleton);
Test.getInstance = function()
{
return Test.supers["Singleton"].getInstance.apply(this,
arguments);
};
Test.prototype.name = null;
Test.prototype.construct = function()
{
switch(arguments.length)
{
case 0:
this.name = "Test";
break;
case 1:
this.name = arguments[0];
break;
}
};
Anyway, I hope that despite this rather terrible explanation I've somehow
managed to get my point across. Any help would be much appreciated.
Thanks.
Matt