Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The perils of the missing javascript block scope

DZone's Guide to

The perils of the missing javascript block scope

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Javascript variable scope is something of a strange thing. You have a global and a function scope. But unlike most other languages, there is no block scope! This is often a cause for hard to find bugs.

Let me show you the consequences of this with a small puzzler:

<html>
<body>
<a id="link1" href="">Link 1</a> <span id="part1"></span><br/>
<a id="link2" href="">Link 2</a> <span id="part2"></span><br/>
<a id="link3" href="">Link 3</a> <span id="part3"></span><br/>
<a id="link4" href="">Link 4</a> <span id="part4"></span><br/>
<a id="link5" href="">Link 5</a> <span id="part5"></span><br/>
No Link 6 <span id="part6"></span><br/>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    for (i = 1; i <= 5; i++) {
      $("#link"+i).click(function() {
        $("#part"+i).html("Clicked !");
        return false;
      });
    }
  });
</script>
</body>
</html>

Now what does this do? (you need a bit of jquery knowledge to guess it)

At first glance, it walks over the 5 links present on this page, and registers a handler for mouse clicks that adds the text "Clicked !" to the span element with the same number as the link.

Or does it?

Well, this is what the output looks like when you click on "Link 3":

Test


It is totally wrong ! Now why is that? Well, as we have no block scope, by the time we are done with the for loop, the variable i has been incremented up to 6. It is however still in scope, and therefore this last value gets used when the closure for the click event gets called. And so it is part 6 that receives the new text !

So how can we fix this?

We have to separate the scopes, and to do that we need a new function. This is what the correct code looks like:
<html>
<body>
<a id="link1" href="">Link 1</a> <span id="part1"></span><br/>
<a id="link2" href="">Link 2</a> <span id="part2"></span><br/>
<a id="link3" href="">Link 3</a> <span id="part3"></span><br/>
<a id="link4" href="">Link 4</a> <span id="part4"></span><br/>
<a id="link5" href="">Link 5</a> <span id="part5"></span><br/>
No Link 6 <span id="part6"></span><br/>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    for (i = 1; i <= 5; i++) {
      addClickEvent(i);
    }
  });
  
  function addClickEvent(i) {
    $("#link"+i).click(function() {
      $("#part"+i).html("Clicked !");
      return false;
    });
  }
</script>
</body>
</html>

With the registration of the click event taking place in a separate function (and therefore a separate scope for the variable i), this is what the output looks like:

Test OK


Which is much more like what we were expecting !

So beware of the lack of block scope in Javascript! It can come and bite more easily than you think if you're not extra careful about it!


Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Axel Fontaine, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}