Wierd syntax for if statement

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

    #1

    Wierd syntax for if statement

    This piece of code is from a JavaScript. The function createOpts takes one
    or more SELECT objects as arguments.


    function createOpts(sel) {
    var s = new Array();
    var num = 0;
    var n = sel.selectedInd ex;
    var args = createOpts.argu ments.length;
    for(var i=1;args>i;i++) {
    s[i-1] = document.getEle mentById(create Opts.arguments[i]);
    if(s[i-1]==sel)num=i;
    }
    if(args>1){
    if(s[num])sel = s[num];
    else return;
    }
    ....
    ....
    ....
    ....
    }


    I don't get the last line in the for-loop. How can one of the arguments that
    are stored in array "s" equal "sel"?

    In the if statment I can't see how "s[num]" can evaluate to true or false.


  • Michael Winter

    #2
    Re: Wierd syntax for if statement

    On 29/05/2005 18:26, JS wrote:

    [snip]
    [color=blue]
    > s[i-1] = document.getEle mentById(create Opts.arguments[i]);
    > if(s[i-1]==sel)num=i;[/color]

    [moved:][color=blue]
    > I don't get the last line in the for-loop. How can one of the
    > arguments that are stored in array "s" equal "sel"?[/color]

    In the preceeding line, an object reference is assigned to an element of
    the array, s. The equality test then examines this object reference, and
    the reference that is sel. If they both refer to the same object, the
    operator will evaluate to true.

    var a = {},
    b = {};

    a != b

    a == a && b == b

    Object equality is performed in terms of the same object; even if all of
    the properties of two objects are the identical, they won't be
    considered equal as they two separate objects.

    [snip]
    [color=blue]
    > if(s[num])sel = s[num];[/color]

    [snip]
    [color=blue]
    > In the if statment I can't see how "s[num]" can evaluate to true or false.[/color]

    All types in ECMAScript can be converted to a boolean:

    string Empty strings ('') are false. All others are true.
    number Zero (0) is false. All other numbers are true.
    boolean No conversion
    null Always false
    undefined Always false
    object Always true
    function Always true



    I said this last time you posted part of this code: accessing the
    arguments object through a reference to a function is deprecated. In
    other words, change occurances of

    createOpts.argu ments

    to

    arguments

    Mike

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

    Comment

    • Lee

      #3
      Re: Wierd syntax for if statement

      JS said:[color=blue]
      >
      > This piece of code is from a JavaScript. The function createOpts takes one
      >or more SELECT objects as arguments.
      >
      >
      >function createOpts(sel) {
      > var s = new Array();
      > var num = 0;
      > var n = sel.selectedInd ex;
      > var args = createOpts.argu ments.length;
      > for(var i=1;args>i;i++) {
      > s[i-1] = document.getEle mentById(create Opts.arguments[i]);
      > if(s[i-1]==sel)num=i;
      > }
      > if(args>1){
      > if(s[num])sel = s[num];
      > else return;
      > }
      >...
      >...
      >...
      >...
      >}
      >
      >
      >I don't get the last line in the for-loop. How can one of the arguments that
      >are stored in array "s" equal "sel"?
      >
      >In the if statment I can't see how "s[num]" can evaluate to true or false.[/color]

      A paragraph written in English is not "an English".
      Code written in Javascript is not "a Javascript".

      The "sel" argument to the function is not a Select object,
      it's a reference to a Select object. If one of the elements
      of s[] is a reference to that same object, then they will
      compare as "equal".

      Any value will evaluate to true or false.
      In the case of s[num], the value will evaluate as "true" if
      there is an element at index "num" with any value other than
      false or zero.

      Comment

      • JS

        #4
        Re: Wierd syntax for if statement

        > > I don't get the last line in the for-loop. How can one of the[color=blue][color=green]
        > > arguments that are stored in array "s" equal "sel"?[/color]
        >
        > In the preceeding line, an object reference is assigned to an element of
        > the array, s. The equality test then examines this object reference, and
        > the reference that is sel. If they both refer to the same object, the
        > operator will evaluate to true.
        >
        > var a = {},
        > b = {};
        >
        > a != b
        >
        > a == a && b == b
        >
        > Object equality is performed in terms of the same object; even if all of
        > the properties of two objects are the identical, they won't be
        > considered equal as they two separate objects.[/color]


        The first time I make a call to createOpts I only use one argument (the
        first select object "sel1"). The second time I call createOpts I use three
        arguments (this, sel1, sel2):

        <body onload="initSel s()"> // a method that just calls createOpts with
        "sel1" as argument.
        <form name="sels" method="post" action="?page=j ubii">
        Two Dependant SELECT forms:&nbsp;&nb sp;&nbsp;
        <select name="sel1" id="sel1" onchange="creat eOpts(this,'sel 1','sel2');">
        </select>&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
        <select name="sel2" id="sel2">
        <option value="" selected[color=blue]
        >-er- Choose --</option>[/color]
        </select>
        &nbsp;
        <input name="search" type="text" id="search">
        &nbsp;
        <input type="submit" name="Submit" value="Search">
        </form>



        But I cannot understand which argument "sel" refers to in :

        for(var i=1;args>i;i++) {
        s[i-1] = document.getEle mentById(create Opts.arguments[i]);
        if(s[i-1]==sel)num=i;
        }


        Comment

        • Danny

          #5
          Re: Wierd syntax for if statement


          You may want to elaborate your query a bit more, too vague to determine,
          js doesn't have a native createOps() method, so, it has to be custom, I
          don't see it posted, .arguments[i] is just picking up arguments off the
          function call and puttting it in a 'S' array, so, it could be in the
          initializer function or elsewhere, but, is more than likely global
          variable thus being called without initizializatio n from any function.

          Danny


          On Sun, 29 May 2005 16:06:13 -0700, JS <dsa.@asdf.co m> wrote:
          [color=blue][color=green][color=darkred]
          >> > I don't get the last line in the for-loop. How can one of the
          >> > arguments that are stored in array "s" equal "sel"?[/color]
          >>
          >> In the preceeding line, an object reference is assigned to an element of
          >> the array, s. The equality test then examines this object reference, and
          >> the reference that is sel. If they both refer to the same object, the
          >> operator will evaluate to true.
          >>
          >> var a = {},
          >> b = {};
          >>
          >> a != b
          >>
          >> a == a && b == b
          >>
          >> Object equality is performed in terms of the same object; even if all of
          >> the properties of two objects are the identical, they won't be
          >> considered equal as they two separate objects.[/color]
          >
          >
          > The first time I make a call to createOpts I only use one argument (the
          > first select object "sel1"). The second time I call createOpts I use
          > three
          > arguments (this, sel1, sel2):
          >
          > <body onload="initSel s()"> // a method that just calls createOpts with
          > "sel1" as argument.
          > <form name="sels" method="post" action="?page=j ubii">
          > Two Dependant SELECT forms:&nbsp;&nb sp;&nbsp;
          > <select name="sel1" id="sel1" onchange="creat eOpts(this,'sel 1','sel2');">
          > </select>&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
          > <select name="sel2" id="sel2">
          > <option value="" selected[color=green]
          >> -er- Choose --</option>[/color]
          > </select>
          > &nbsp;
          > <input name="search" type="text" id="search">
          > &nbsp;
          > <input type="submit" name="Submit" value="Search">
          > </form>
          >
          >
          >
          > But I cannot understand which argument "sel" refers to in :
          >
          > for(var i=1;args>i;i++) {
          > s[i-1] = document.getEle mentById(create Opts.arguments[i]);
          > if(s[i-1]==sel)num=i;
          > }
          >
          >[/color]



          --
          Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

          Comment

          Working...