sg

Perşembe, Aralık 08, 2005

Re: JavaScript vars question


Wasn't it Dickies who wrote:
>
>Got a quick question about this page:
>http://www.econym.demon.co.uk/googlemaps/examples/map3.htm
>
>In the source there is a global var set to 0:
>var i = 0;

That "i" is important for the line

sidebar_html += '<a href="javascript:myclick(' + i + ')">'
+ name + '</a><br>';

If it wasn't for that, then I wouldn't have bothered with the variable
and just written
gmarkers.push(marker);
htmls.push(html);
instead of
gmarkers[i] = marker;
htmls[i] = html;
It is actually possible to rewrite the sidebar_html line without "i",
but I think it's more readable as it is.

>Also in the loop to add markers another i var is declared:
>for (var i = 0; i < markers.length; i++) {

In Javascript you can have local variables which are only accessible
inside a particular function. In this case, this "i" variable is local
to the anonymous function that starts with this line
request.onreadystatechange = function() {

The two "i" variables are completely separate.

Looking at it now, I guess that at any instant those two "i" variables
happen to hold the same number. I hadn't noticed that when I wrote it.
The code doesn't rely on their being any relationship between them.

One of the nice features about local variables is that I can write that
anonymous function without having to worry about conflict with variables
with the same name that have different scope. Most computer languages
have this facility. In primitive computer languages, like some simple
dialects of BASIC, and primitive Assembler codes, which don't have
scoped variables it's harder for teams of programmers to work together
on large programs because of the potential for such conflicts.

--
The Blackpool Community Church Javascript Team
http://www.econym.demon.co.uk/googlemaps/

0 Comments:

Yorum Gönder

<< Home


Komik Videolar   islam  şarkı sözleri  yemek tarifleri  gelibolu  huzur   sağlık