473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Where is the For Next in the function

Claus Mygind
571 512MB
I am having some trouble understanding the syntax in an anonymous function.

I understand that a for next loop is constructed like this
Expand|Select|Wrap|Line Numbers
  1. for (var i = 0  i < someNumber  i++)
  2. {
  3.   code in here get repeated for each time in loop
  4. }
  5.  
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
Expand|Select|Wrap|Line Numbers
  1. google.maps.event.addListener(marker, 'click', function() {
  2.   marker.setMap(null);
  3.   for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
  4.     markers.splice(i, 1);
  5.     path.removeAt(i);
  6. }
  7. );
  8.  
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:
Expand|Select|Wrap|Line Numbers
  1. google.maps.event.addListener(marker, 'rightclick', function() {
  2.   for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
  3.     thisMarkerNum = i;
  4.   (document.getElementById("propertyTable").style.visibility == "hidden" ) ? showPropTable(marker, "boundry marker "+marker.title, thisMarkerNum):hidePropTable();
  5. });
  6.  
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?
Oct 22 '10 #1

✓ answered by Plater

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

11 1621
Plater
7,872 Expert 4TB
I believe this line:
Expand|Select|Wrap|Line Numbers
  1. for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i); 
  2.  
is the same as
Expand|Select|Wrap|Line Numbers
  1. for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i)
  2. {
  3. }
  4.  
Oct 22 '10 #2
Claus Mygind
571 512MB
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.
Oct 25 '10 #3
Plater
7,872 Expert 4TB
Well this statement is completely normal syntax:
Expand|Select|Wrap|Line Numbers
  1. if (a==b)
  2.    alert('Values are the same!');
  3.  
and is the same as
Expand|Select|Wrap|Line Numbers
  1. if (a==b)
  2. {
  3.    alert('Values are the same!');
  4. }
  5.  
The {} just denotes a grouping of code (it is more complex then that, but basically)
Oct 25 '10 #4
Claus Mygind
571 512MB
So in this example
Expand|Select|Wrap|Line Numbers
  1.  google.maps.event.addListener(marker, 'click', function() {
  2.    marker.setMap(null);
  3.    for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
  4.      markers.splice(i, 1);
  5.      path.removeAt(i);
  6.  });
  7.  
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 { }
Oct 25 '10 #5
Plater
7,872 Expert 4TB
Hmm, with that semi colon at the end i would have assumed NONE of them are part of the loop
Oct 25 '10 #6
Claus Mygind
571 512MB
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
Nov 1 '10 #7
Dormilich
8,658 Expert Mod 8TB
maybe it’s just a typo and they meant { instead of ;
Nov 2 '10 #8
Claus Mygind
571 512MB
That would leave the leading { missing
Nov 5 '10 #9
Claus Mygind
571 512MB
sorry meant the } missing. gotta go in pairs
Nov 5 '10 #10
Dormilich
8,658 Expert Mod 8TB
otherwise it would have thrown an error.
Nov 5 '10 #11
gits
5,390 Expert Mod 4TB
the shown construct could make sense
Expand|Select|Wrap|Line Numbers
  1. 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
Nov 5 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Chris Bedford | last post by:
Hello, I'm using Xalan and I have a bunch of xslt documents that were constructed to run with another processor that has alot of custom extension functions. The names of these functions ...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
6
by: Csaba Gabor | last post by:
I'd like to be able to pass a key1/value1 pair into a function and have that function have a local variable by the name of key1 to which value1 is assigned. for example, I'd like to call...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
2
by: Sean | last post by:
Hi all, I know that some variables are stored on heaps and some on stack in C++. How about functions? where does function reside on memory? Is it stack or heap? And are both function (not...
5
by: Anane | last post by:
In my application I have 3 classes. 1. class A 2. class B 3. class C Application controller (having main function) has the object of class A, class A has object of class B and class B has...
1
by: phpnewbie26 | last post by:
So I'm trying to use the next() function on my array within my multidimensional array. It looks like this: Array( testA=> Array( phaseA=>5, phaseB=>4, ...
0
by: mustakbal | last post by:
I am using this code to search with wild cards for strings in a text file. I wanted to improve the application by adding a "Find Next " button that will display the rest of the strings that the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.