for (.. in ..) not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • noticias.mad.ttd.net

    #1

    for (.. in ..) not working

    Hello.
    Some days ago I posted a message about this (somebody replied asking me
    if I was using IE, but I am using Mozilla 1.7.5):

    This 'for' statment works for me, i.e.:

    for (var i = aElts.length; i--; ){
    var oElement = aElts[i];
    if (oElement.id)
    if (oElement.id == 'alert2')
    oElement.onclic k = Alert;
    }
    }

    But why this doesn't work:

    for (oElement in aElts){
    var oElement = aElts[i];
    if (oElement.id)
    if (oElement.id == 'alert2')
    oElement.onclic k = Alert;
    }
    }

    Why?
    I am attaching two testcases (testcase A works, testcase B doesn't work;
    and the JS Console is not warning about anything).

    Thanks in advance.

    knocte

  • knocte

    #2
    Re: for (.. in ..) not working

    I wrote:[color=blue]
    > ...
    > But why this doesn't work:
    >
    > for (oElement in aElts){
    > var oElement = aElts[i];
    > if (oElement.id)
    > if (oElement.id == 'alert2')
    > oElement.onclic k = Alert;
    > }
    > }[/color]

    Sorry, mistake: the correct example is:

    for (oElement in aElts){
    if (oElement.id)
    if (oElement.id == 'alert2')
    oElement.onclic k = Alert;
    }
    }

    Regards.

    Comment

    • Martin Honnen

      #3
      Re: for (.. in ..) not working



      knocte wrote:

      [color=blue]
      > the correct example is:
      >
      > for (oElement in aElts){
      > if (oElement.id)
      > if (oElement.id == 'alert2')
      > oElement.onclic k = Alert;
      > }
      > }[/color]

      The for .. in loop enumerates the property names of enumerable
      properties of an object so for that purpose it is meant and useful (at
      least sometimes for user defined objects).
      If you have a collection then in JavaScript it usually has a length
      property and allows access to elements in the collection with [index]
      and/or .item(index) so you need a normal for loop and not a for .. in loop.
      There are other languages with for .. in loop constructs which might be
      syntactically similar (or even equal) to the one JavaScript has but of
      course you shouldn't then assume that what works in one language for you
      has to work in JavaScript the same way.

      JavaScript is standardized as ECMAScript, the current edition is edition
      3. In 2004 an extension to ECMAScript called E4X (ECMAScript for XML)
      has been standardized, it adds an additional for each .. in loop
      statement to the language which might be closer to what you are looking
      for in making the property value available in the loop and not the
      property names. However it then applies to all objects and not only
      collections and depending on the properties a collection has and whether
      they are marked as enumerable even a
      for each (var prop in collection) {
      // use prop here
      }
      is not guaranteed to only give you those properties you regard as
      elements in the collection. A recent Mozilla 1.8 nightly build when
      executing
      <script type="text/javascript; e4x=1">
      should have support for the for each .. in statement.

      --

      Martin Honnen
      http://JavaScript.FAQTs.com/

      Comment

      • Richard Cornford

        #4
        Re: for (.. in ..) not working

        noticias.mad.tt d.net wrote:
        <snip>[color=blue][color=green]
        >> I am attaching two testcases ...[/color][/color]

        Please don't post attachments to comp.lang.javas cript. This is a plain
        text only group, and many news servers will remove attachments
        automatically. See the group's FAQ.

        Richard.


        Comment

        • Richard Cornford

          #5
          Re: for (.. in ..) not working

          knocte wrote:[color=blue]
          > for (oElement in aElts){
          > if (oElement.id)[/color]

          The - for (prop in obj) - construct assigns a sequence of string
          property names to the first identified variable (- prop -). When a
          string primitive is the left hand operand of the dot operator (in a
          property accessor) that string primitive is type-converted into a String
          object. String objects do not have - id - properties so the value read
          with the property accessor is - Undefined - (the special ECMAScript -
          Undefined - value), and - Undefined - type-converts to boolean false for
          the - if - statement.
          [color=blue]
          > if (oElement.id == 'alert2')
          > oElement.onclic k = Alert;
          > }
          > }[/color]

          So, the execution of the code never enters the outermost - if - block,
          but not errors are expected.

          Richard.


          Comment

          • Jim Ley

            #6
            Re: for (.. in ..) not working

            On Sun, 30 Jan 2005 19:24:03 +0100, Martin Honnen <mahotrash@yaho o.de>
            wrote:
            [color=blue]
            >is not guaranteed to only give you those properties you regard as
            >elements in the collection. A recent Mozilla 1.8 nightly build when
            >executing
            > <script type="text/javascript; e4x=1">[/color]

            Please don't encourage any further pollution of the text/javascript
            mime-type, application/x-javascript;e4x= 1 works and makes a lot more
            sense.

            Jim.

            Comment

            • knocte

              #7
              Re: for (.. in ..) not working

              Richard Cornford escribió:
              [color=blue]
              > noticias.mad.tt d.net wrote:
              > <snip>
              >[color=green][color=darkred]
              >>>I am attaching two testcases ...[/color][/color]
              >
              >
              > Please don't post attachments to comp.lang.javas cript. This is a plain
              > text only group, and many news servers will remove attachments
              > automatically. See the group's FAQ.
              >
              > Richard.
              >
              >[/color]

              Sorry for my netiquette lack, but I thought that adding two minimal HTML
              attachments (testcases) would not hurt anybody.

              Thanks for all replies.

              Regards.

              Comment

              • Lee

                #8
                Re: for (.. in ..) not working

                knocte said:[color=blue]
                >
                >Richard Cornford escribió:
                >[color=green]
                >> noticias.mad.tt d.net wrote:
                >> <snip>
                >>[color=darkred]
                >>>>I am attaching two testcases ...[/color]
                >>
                >>
                >> Please don't post attachments to comp.lang.javas cript. This is a plain
                >> text only group, and many news servers will remove attachments
                >> automatically. See the group's FAQ.
                >>
                >> Richard.
                >>
                >>[/color]
                >
                >Sorry for my netiquette lack, but I thought that adding two minimal HTML
                >attachments (testcases) would not hurt anybody.
                >
                >Thanks for all replies.[/color]

                The fact that there were attachments changed your post from plain text
                to MIME format. You could have simply included the HTML as text in
                the message.

                Comment

                Working...