Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 21st, 2010, 06:37 PM
Registered User
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 7 Closures and Variables Question

On page 188 there is the following code:

function createFunctions(){
var result = new Array();
for (var i=0; i < 10; i++){
result[i] = function(){
return i;
return result;

The explanation paragraph that follows is confusing, it states the following:

'This function returns an array of functions. It seems that each function should just return the value of its index, so the function in position 0 returns, the function in position 1 returns 1, and so on. In reality, every function returns 10.'

The first part makes sense in that the function returns an array of functions, but as far as each index holding 10 is concerned they don't actually hold that. I tried calling the function to see what value each actually holds like so:

var theResult = createFunctions();

The value you get is the actual function itself for each index not the value of 10 for each index that is allegedly supposed to be return, I tried running the eval() method on the theResult variable instead but the result was the same. So what is actually going on? Have I missed something here?

Also with the so called solution to the problem the same thing happens, solution in book as follows:

function createFunctions(){
var result = new Array();
for (var i=0; i < 10; i++){
result[i] = function(num){
return function(){
return num;
return result;

Also how is num actually getting the i variable, is it due to the line }(i); as that is the final variable within the enclosure of the containing function?
Finally how is the anonymous function being called immediately as stated in the book, what line is actually calling it?

The book so far has been excellent but this specific section of closures and variables has confused the heck out of me.

Please help.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 15 Session variables mcauliff BOOK: Beginning ASP 3.0 3 December 9th, 2005 05:12 PM
Newbie...probably a simple question...variables IP076 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 November 30th, 2004 03:24 PM
HELP - C# newbie question on variables & propertie savoym C# 6 February 6th, 2004 03:52 PM
JSP 2.0 book question...re: variables troutguy All Other Wrox Books 1 August 5th, 2003 05:41 PM

All times are GMT -4. The time now is 06:48 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.