By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Function's arguments

P: n/a
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

Jul 19 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a


sf****@gmail.com wrote:

Is there a way to preserve the arguments across functions?

I have:

<script>
function myFirstFunction()
{
// arguments[0] = 'param1'
// arguments[1] = 'param2'
//...
notMyFunction(arguments);
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:Funct ion:apply>
IE 5 with JScript 5 does not have that however.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 19 '06 #2

P: n/a
you could initialize the argument variables outside of any functions,
which will make them global.

var arguments = new Array;
function ....

Jul 19 '06 #3

P: n/a
you could initialize the argument variables outside of any functions,
which will make them global.

var arguments = new Array;
function ....

Jul 19 '06 #4

P: n/a
Martin Honnen wrote:
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:Funct ion: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/archi..._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

Jul 19 '06 #5

P: n/a

Sevinfooter wrote:
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.

Jul 19 '06 #6

P: n/a
web.dev wrote:
Sevinfooter wrote:
you could initialize the argument variables outside of any functions,
which will make them global.

var arguments = new Array;
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 = [];
function ....
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[i] = arguments[i];
} 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;

[...]
--
Rob

Jul 20 '06 #7

P: n/a
RobG wrote:
web.dev wrote:
<snip>
>>var arguments = new Array;

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.

Jul 20 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.