Where is the For Next in the function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Where is the For Next in the function

    I am having some trouble understanding the syntax in an anonymous function.

    I understand that a for next loop is constructed like this
    Code:
    for (var i = 0  i < someNumber  i++)
    {
      code in here get repeated for each time in loop
    }
    So the curly brackets defines the start and end of code that is repeated each time through the loop.

    What I don't get is where is the start and end of the loop in this example
    Code:
    google.maps.event.addListener(marker, 'click', function() {
      marker.setMap(null);
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
    	markers.splice(i, 1);
    	path.removeAt(i);
    }
    );
    There are curly brackets that define the anonymous function, but where are the curly brackets for the for/next loop?

    I am trying to construct my own event listner like this:
    Code:
    google.maps.event.addListener(marker, 'rightclick', function() {
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
    	thisMarkerNum = i;
      (document.getElementById("propertyTable").style.visibility == "hidden" ) ? showPropTable(marker, "boundry marker "+marker.title, thisMarkerNum):hidePropTable();
    });
    I want to capture i and send it as a parameter. Is my if statement part of the loop or what? And why don't we use curly brackets for the for/next loop?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I believe this line:
    Code:
    for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
    is the same as
    Code:
    for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i)
    {
    }

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      Yep! I gather that. but the curly brackets denote a start and a finish to the code within the loop. I don't get how the one without the brackets even works and does not throw an error.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well this statement is completely normal syntax:
        Code:
        if (a==b)
           alert('Values are the same!');
        and is the same as
        Code:
        if (a==b)
        {
           alert('Values are the same!');
        }
        The {} just denotes a grouping of code (it is more complex then that, but basically)

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          So in this example
          Code:
           google.maps.event.addListener(marker, 'click', function() {
             marker.setMap(null);
             for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
               markers.splice(i, 1);
               path.removeAt(i);
           });
          would we assume only the first line is part of the loop or are both lines part of the loop.

          Or is it just bad code and should really be enclosed in the { }

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Hmm, with that semi colon at the end i would have assumed NONE of them are part of the loop

            Comment

            • Claus Mygind
              Contributor
              • Mar 2008
              • 571

              #7
              Ok I will have to investigate that further that makes sense even though it does not seem proper to write a for next loop without the curly brackets.

              Thanks for the reply

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                maybe it’s just a typo and they meant { instead of ;

                Comment

                • Claus Mygind
                  Contributor
                  • Mar 2008
                  • 571

                  #9
                  That would leave the leading { missing

                  Comment

                  • Claus Mygind
                    Contributor
                    • Mar 2008
                    • 571

                    #10
                    sorry meant the } missing. gotta go in pairs

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      otherwise it would have thrown an error.

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5388

                        #12
                        the shown construct could make sense
                        Code:
                        for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
                        as we could notice it increments the counter-variable i from 0 until it finds the marker in the markers-array ... so it seems to be used to gather the index of the marker(-object) in the markers-array - probably for the splice-operation later on. basically it is a proper construct.

                        kind regards,
                        gits

                        Comment

                        Working...