Function's arguments

S

sfeher

Hi All,

Is there a way to preserve the arguments across functions?

I have:

<script>
function myFirstFunction()
{
// arguments[0] = 'param1'
// arguments[1] = 'param2'
//...
notMyFunction(arguments);
}

function notMyFunction()
{
// arguments[0][0] = 'param1'
// arguments[0][1] = 'param2'
//...
mySecondFunction(arguments);
}

function mySecondFunction(params)
{
// params[0][0] = 'param1'
// params[0][1] = 'param2'
//...
}

myFirstFunction('param1', 'param2', 'param3');

</script>

What I'd like to receive is params[1] instead of params[0][1] since
params[1] never holds anything.

I cannot change notMyFunction() so my question is: is there a way of
calling notMyFunction() from myFirstFunction() and pass the arguments
expanded ?! So far my guess is that I can't do this but it would be
nice if there was something like it.

Regards,
Sebastian
 
S

Sevinfooter

you could initialize the argument variables outside of any functions,
which will make them global.

var arguments = new Array;
function ....
 
S

Sevinfooter

you could initialize the argument variables outside of any functions,
which will make them global.

var arguments = new Array;
function ....
 
S

sfeher

Martin said:
You want the apply method of functions e.g.
notMyFunction.apply(this, arguments);
see
<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function:apply>


IE 5 with JScript 5 does not have that however.

Martin, many thanks!, This is what I was looking for.

For IE6/apply I found this post by Laurens van den Oever on quirksmode:
http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
#61.

// Ancient browser compatibility
if (!Function.prototype.apply)
{
Function.prototype.apply = function (obj, args)
{
obj.___fn = this;
var str = "";
for (var i = 0; i < args.length; i++)
str += (i != 0 ? "," : "") + "args[" + i + "]";
eval("var result = obj.___fn(" + str + ");");
obj.___fn = null;
return result;
};
}

The script seems to do the trick for IE ..

Regards,
Sebastian
 
W

web.dev

Sevinfooter said:
you could initialize the argument variables outside of any functions,
which will make them global.

var arguments = new Array;
function ....

No, you can not do this. If you actually tried testing your code
before posting, then you would've realized this. The arguments
variable you've declared and the functionName.arguments object are in
different scopes. Even if the OP had assigned the
functionName.arguments object to the global variable, it would not do
what you expected.

For example:

var arguments = "Should be overwritten";

function foo()
{
arguments = foo.arguments;

alert(typeof arguments);
}

foo("arg1", "arg2", "arg3");
alert(typeof arguments);

You'll notice the global variable type has not changed even after the
supposed assignment.
 
R

RobG

You can call the Array function as a constructor using:

new Array();
new Array;
Array();
Array;

However the first version is the most popular. It is usually simpler
to use an initialiser:

var arguments = [];

Pitty the rest of the code wasn't supplied, it is left to the
imagination. Anyhow, declaring the global variable 'arguments' as an
Array suggests that within the function something like the following
will be used:

function foo(arg1, arg2){
var i = arguments.length;
do {
window.arguments = arguments;
} while (i--);

// rest of function

}

It could also suggest the belief that a function's arguments object is
an Array, which it isn't. It is much simpler to just assign a
reference to the function's arguments object, in which case whatever
the global arguments variable was initialised to is irrelevant - it may
as well be declared without initialisation:

var arguments;

No, you can not do this.

Yes you can, just not exactly the way you've tried it. :)

[...]
The arguments
variable you've declared and the functionName.arguments object are in
different scopes.

Yes, but you forgot deal with that in your example.

Even if the OP had assigned the
functionName.arguments object to the global variable, it would not do
what you expected.

Supposing the OP did do that, then you should be not be saying 'even
if' but 'because'. If Sevinfooter had provided more code, we'd know
whether your guess was right or wrong - giving Sevinfooter the benefit
of doubt, you guessed wrong.

For example:

var arguments = "Should be overwritten";

function foo()
{
arguments = foo.arguments;

When resolving variable names, JavaScript first looks in the local
scope. When the code is called, arguments refers to the local arguments
object, as does foo.arguments (though normally it is referenced as
simply 'arguments') so they both reference the same local object and
the global arguments property is not modified.

The 'fix' is to either change the name of the global variable so it
isn't masked by a local variable of the same name, or refer to it
explicitly:

window.arguments = arguments;

[...]
 
R

Richard Cornford

RobG said:
web.dev wrote:

You can call the Array function as a constructor using:

new Array();
new Array;
Array();
Array;
<snip>

Not the last. Array - just evaluates to a reference to the array
constructor function, and that evaluation has no side effects.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top