Wrox Programmer Forums
|
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 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
 
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();
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 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





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





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