473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Function's arguments

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
7 1918


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
you could initialize the argument variables outside of any functions,
which will make them global.

var arguments = new Array;
function ....

Jul 19 '06 #3
you could initialize the argument variables outside of any functions,
which will make them global.

var arguments = new Array;
function ....

Jul 19 '06 #4
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chuck Anderson | last post by:
I have a function with 7 inputs. The last three have default values. I want to call that function specifying the first four, skip two and then specify the last. I thought I could write this...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.