Wrox Programmer Forums
| 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 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
  #11 (permalink)  
Old July 11th, 2006, 05:28 PM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nicholas,

So closures can't be created if you only use the Function constructor to create new functions.

Is there a situation where using the contructor is better?

Thanks

Christian
  #12 (permalink)  
Old July 11th, 2006, 06:04 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

That's not entirely a correct statement. The rule is:

You cannot capture variables outside of function scope when using the Function constructor. You can create closures, like this:

Code:
var myFunc = new Function("var a=0; return function () { return a; }");
Technically, this function creates a function that is a closure (it captures the variable "a").

Generally, it's considered poor practice to use the Function constructor to create new functions, though technically it's not wrong.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
  #13 (permalink)  
Old July 11th, 2006, 06:26 PM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nicholas,

What I meant was that if you used the function constructor *everytime* you wanted to define a function you wouldn't be able to do it.

If I take your code from the last post and change how you created the internal function to also use the function constructor, then it wouldn't be a closure anymore.

I really appreciate the time you take to answer my crazy questions.

Thanks

Christian
  #14 (permalink)  
Old July 11th, 2006, 06:33 PM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nicholas,

Wouldn't the code on your last post return a reference to a function rather than executing the anonymous function and therefore returning the value of the variable a?

Christian




Similar Threads
Thread Thread Starter Forum Replies Last Post
C functions...??? ethantinder C# 2 April 25th, 2008 02:25 AM
Functions LTello BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 3 September 15th, 2006 11:15 AM
functions xelepi PHP How-To 1 March 10th, 2006 02:59 AM
Functions Meg Intro Programming 1 January 23rd, 2006 01:03 PM
Help with Functions megabytes C# 1 August 7th, 2003 12:48 PM





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