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