functions and arguments.length; passing unknown number of arguments

J

Joost Diepenmaat

oldyork90 said:
I'm going thru code and have never seen this before
http://www.webreference.com/programming/javascript/mk/column2/3.html


Look at function CreateDragContainer() on line 25. It has no
arguments defined and depends on a function property named arguments
to process its input. I poked around and found this is deprecated.

Function.arguments is deprecated. The automically instantiated variable
arguments is not.
How do you pass an unknown number of arguments to a function? Put
them in an array?

I use the arguments variable.
 
S

SAM

Le 9/24/08 9:23 PM, oldyork90 a écrit :
How do you pass an unknown number of arguments to a function? Put
them in an array?

function hello() {
for(var i=0; i< arguments.length; i++) alert(arguments);
}

hello('hello guy');
hello('hello','old boy');
hello('hello','Old Rock','the 90th');

var t1 = 'Hello';
var t2 = 'man';
hello(t1, t2);


Is there really necessary to put previously the arguments in an array ?


var or = ['hello','Old Rock','the 90th'];

function salut() {
arguments = arguments.length==1?
arguments[0].toString().split(',') : arguments;
for(var i=0; i< arguments.length; i++) alert(arguments);
}

salut(or);
 
K

Kiran Makam

How do you pass an unknown number of arguments to a function? Put
them in an array?

You can use an object (with necessary properties initialized) to pass
the arguments.
For example, if your function requires 5 arguments: arg1, arg2...arg5

function myFn(oArgs){
var arg1 = oArgs.arg1;

//initialize if the arg is not passed
var arg2 = (oArgs.arg2 == undefined)? "myDefaultValue" : oArgs.arg2;
}

//function call
myFn(
{
arg1: "xxx",
arg2: "yyy",
...
arg5: "zzz"
}
);

- Kiran Makam
 
T

Thomas 'PointedEars' Lahn

Joost said:
Function.arguments is deprecated. The automically instantiated variable
arguments is not.


I use the arguments variable.

`arguments' is a property of the Activation/Variable Object of a local
execution context, but not a variable. And no, that is not a contradiction :)


PointedEars
 
J

Joost Diepenmaat

Thomas 'PointedEars' Lahn said:
`arguments' is a property of the Activation/Variable Object of a local
execution context, but not a variable. And no, that is not a contradiction :)

In the interest of academics; is there any way to tell the difference
using javascript code?
 
T

Thomas 'PointedEars' Lahn

Joost said:
In the interest of academics; is there any way to tell the difference
using javascript code?

Quick hack for testing if `x' was declared a local variable:

/\bvar\s+([A-Za-z_$][\w$]*\s*,\s*)*x\b/.test(arguments.callee)

This should be adapted for Unicode-aware implementations, see ES3F 12.2 and
7.5.3.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Quick hack for testing if `x' was declared a local variable:

/\bvar\s+([A-Za-z_$][\w$]*\s*,\s*)*x\b/.test(arguments.callee)

This should be adapted for Unicode-aware implementations, see ES3F 12.2 and
7.5.3.
^^^^^
I meant the following section, 7.6 ("Identifiers"), of course.

Thank you and good night ;-)


PointedEars
 
J

Jorge

In the interest of academics; is there any way to tell the difference
using javascript code?

Quick hack for testing if `x' was declared a local variable:

  /\bvar\s+([A-Za-z_$][\w$]*\s*,\s*)*x\b/.test(arguments.callee)

Parsing the source ? come on ! That's not serious !

Try to see a bit further than your own nose. Activation objects are
notional entities. They could as well have called them 'magically
bound variables'.
 
T

Thomas 'PointedEars' Lahn

Jorge said:
Thomas said:
Joost said:
Joost Diepenmaat wrote:
Function.arguments is deprecated. The automically instantiated variable
arguments is not.
How do you pass an unknown number of arguments to a function? Put
them in an array?
I use the arguments variable.
`arguments' is a property of the Activation/Variable Object of a local
execution context, but not a variable. And no, that is not a contradiction :)
In the interest of academics; is there any way to tell the difference
using javascript code?
Quick hack for testing if `x' was declared a local variable:

/\bvar\s+([A-Za-z_$][\w$]*\s*,\s*)*x\b/.test(arguments.callee)

Parsing the source ? come on ! That's not serious !

If you have a better idea, then post it. If not, shut up.
Try to see a bit further than your own nose. Activation objects are
notional entities.

Whatever you might mean by "notional", Activation Objects exist and are part
of the scope chain of function code (ES3F, 10.2.3). Furthermore, "The
activation object is then used as the variable object for the purposes of
variable instantiation." (10.1.6) Unfortunately, the Activation/Variable
Object cannot be referred to, hence this quick hack.
They could as well have called them 'magically bound variables'.

You could as well be called an incompetent fool, but I will assume in your
favor you are just tired or drunk or both right now. So go to bed, please.


PointedEars
 
J

Jorge

Whatever you might mean by "notional", Activation Objects exist and are part
of the scope chain of function code (ES3F, 10.2.3).  Furthermore, "The
activation object is then used as the variable object for the purposes of
variable instantiation." (10.1.6)  Unfortunately, the Activation/Variable
Object cannot be referred to, hence this quick hack.

And rest assured that nearly everything everywhere nowadays is
(internally) held in objects as well... and ? 'If it looks like a duck
and quacks like a duck, it must be a duck' : but this object of yours,
from a JS programmer point of view, not only doesn't quack like an
object, it doesn't even look like one because it's invisible. They
might have as well written the spec in another way using a different
language and you would not be here arguing about its existence. In
this sense it's 'notional only'.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top