Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 2nd Edition by Nicholas C Zakas; ISBN: 9780470227800
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 22nd, 2010, 07:39 AM
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 it doesn't actually hold do that. I tried calling the function to see what value each actually holds like so:

var theResult = createFunctions();
alert(theResult);

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;
};
}(i);
}
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 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.

Thanks
  #2 (permalink)  
Old July 22nd, 2010, 06:32 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Hi Mike,

You're testing the result incorrectly. The way you should be testing it is:
Code:
var functions = createFunctions();

alert(functions[0]());   //10
alert(functions[1]());   //10

//etc.
What you're doing is just alerting the array of functions, which will appear to be the same in both cases.

To answer your other questions: i is passed to num via (i). The anonymous function is being immediately called by placing parentheses at the end. i is the value being passed into the function and num is the name of the argument for that function.
__________________
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
  #3 (permalink)  
Old July 28th, 2010, 05:26 PM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Excellent stuff, problem solved, thanks!!!


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 7 Closures and Variables Question Mikeos BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 0 July 21st, 2010 06:37 PM
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





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