Wierd call on methods

J

JS

I read the following in som sample code:

function createOpts(sel){
var s = new Array();
var num = 0, txt = "bo";
var ar = opt = null;
var n = sel.selectedIndex;
var args = createOpts.arguments.length;
....
....
....
....

The createOpts method that a select object as argument. Creates a new Array:

When no argument is specified for an Array I guess its just empty with size
1.

The line that says:

var args = createOpts.arguments.length;

Is it correct that args holds the number of options that the selectobject
contains??
 
R

Randy Webb

JS said:
I read the following in som sample code:

function createOpts(sel){
var s = new Array();
var num = 0, txt = "bo";
var ar = opt = null;
var n = sel.selectedIndex;
var args = createOpts.arguments.length;
....
....
....
....

The createOpts method that a select object as argument. Creates a new Array:

When no argument is specified for an Array I guess its just empty with size
1.

Its empty with a length of 0. Arrays have no "size" property.
The line that says:

var args = createOpts.arguments.length;

Is it correct that args holds the number of options that the selectobject
contains??

No, args contains a number that relates to the length of a possible list
of arguments passed to a function.

try this in your browser:

function checkArguments(args){
alert(checkArguments.arguments.length)
}

checkArguments(1);
checkArguments(1,2);
checkArguments(1,"2","string");
 
M

Michael Winter

On 26/05/2005 23:25, JS wrote:

[snip]
var args = createOpts.arguments.length;

This should be

var args = arguments.length;

The arguments object should be accessed directly, not via a function object.
Is it correct that args holds the number of options that the selectobject
contains??

No. The arguments object represents the arguments passed to a function
when it's called. It will look like an array, but it isn't: it only has
two named properties, length and callee, and a series of numbered
properties starting at zero (0). For example,

/* Declaration */
function myFunction(arg0, arg1) {}

/* Call */
myFunction('string', 15, false);

The function, myFunction, is called with three arguments, but there are
only two named (or formal) arguments in the declaration. So, how do we
access the third? Using the arguments object.

In this example, arguments will have five properties in total:

arguments[0] The value of the first argument (arg0): 'string'
arguments[1] ...of the second argument (arg1): 15
arguments[2] ...of the third (it has no name): false
arguments.length The number of arguments passed to the function: 3
arguments.callee A reference to the function, myFunction, itself.

Does that make sense?

Presumably, createOpts allows any number of extra arguments after the
first one, sel. These extra arguments will probably be the display text
and values for the new OPTION elements. Rather than creating a long,
pointless list like

function createOpts(sel, text1, value1, text2, value2, ...) {}

which become almost impossible to work with, it just takes the extra
arguments from the arguments object.

Mike
 

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,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top