Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Strange Scoping Problem
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
shaneal  
View profile  
 More options Feb 17 2008, 3:29 am
Newsgroups: comp.lang.javascript
From: shaneal <sma...@gmail.com>
Date: Sun, 17 Feb 2008 00:29:58 -0800 (PST)
Local: Sun, Feb 17 2008 3:29 am
Subject: Strange Scoping Problem
Hello all,

I've been trying to learn some javascript and I ran into a strange
scoping problem I was hoping someone here could help with. I have a
fair amount of experience with functional programming, but very little
with javascript, so please excuse the unidiomatic code.

When I do:

var functions = new Array();

function printNum(x) {
   alert(x);

}

for(var i=0;i<5;i++) {
   functions[i] = function() { printNum(i);}

}

Now, I'd not expect
functions[0]();
to alert with 0, but instead it alerts with 5. Why is that, and how
can I work around it?

I was banging my head on a strange problem for a bit, until I finally
managed to track down the problem to something isomorphic to this bit
of code.

PS: functions[1-4](); all alert with 5 too ... in case that is
relevant

Thanks for the help, and I apologize in advance for any breaches of
protocol I've made here (first time on comp.lang.javascript).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Mark  
View profile  
 More options Feb 17 2008, 3:46 am
Newsgroups: comp.lang.javascript
From: David Mark <dmark.cins...@gmail.com>
Date: Sun, 17 Feb 2008 00:46:58 -0800 (PST)
Local: Sun, Feb 17 2008 3:46 am
Subject: Re: Strange Scoping Problem
On Feb 17, 3:29 am, shaneal <sma...@gmail.com> wrote:

Because i *is* 5 after the loop is finished.  All of the functions
created are referencing the same i variable.   If the for loop is run
in the global context, then i is a global variable.  If not, i is a
local variable and part of a closure formed by assigning references to
the functions to the global functions array.

Something like this will produce the desired results:

functions[i] = (function(j) { return function() { printNum(j); }; })
(i);

That pattern creates a distinct closure for each of the five
functions.

> I was banging my head on a strange problem for a bit, until I finally

Scope-related issues (particularly closures) are a common source of
confusion for beginning JS programmers.

[snip]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
shaneal  
View profile  
 More options Feb 17 2008, 5:11 am
Newsgroups: comp.lang.javascript
From: shaneal <sma...@gmail.com>
Date: Sun, 17 Feb 2008 02:11:37 -0800 (PST)
Local: Sun, Feb 17 2008 5:11 am
Subject: Re: Strange Scoping Problem
ah, many thanks. your explanations make perfect sense.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »