Wierd call on methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JS

    #1

    Wierd call on methods

    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.selectedInd ex;
    var args = createOpts.argu ments.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.argu ments.length;

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



  • Randy Webb

    #2
    Re: Wierd call on methods

    JS wrote:
    [color=blue]
    > 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.selectedInd ex;
    > var args = createOpts.argu ments.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.[/color]

    Its empty with a length of 0. Arrays have no "size" property.
    [color=blue]
    > The line that says:
    >
    > var args = createOpts.argu ments.length;
    >
    > Is it correct that args holds the number of options that the selectobject
    > contains??[/color]

    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(checkArgu ments.arguments .length)
    }

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



    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

    Comment

    • Michael Winter

      #3
      Re: Wierd call on methods

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

      [snip]
      [color=blue]
      > var args = createOpts.argu ments.length;[/color]

      This should be

      var args = arguments.lengt h;

      The arguments object should be accessed directly, not via a function object.
      [color=blue]
      > Is it correct that args holds the number of options that the selectobject
      > contains??[/color]

      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('str ing', 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.lengt h The number of arguments passed to the function: 3
      arguments.calle e 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

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      Working...