Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | 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
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 7th, 2006, 10:57 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default Functions

On page 89 there is the following code:
Code:
var oCar = new Object;
oCar.color = “red”;
oCar.showColor = function () {
alert(this.color); //outputs “red”
};
I was wondering if there is any difference between using
Code:
oCar.showColor = function () {
alert(this.color); //outputs “red”
};
AND

Code:
oCar.showColor = new Function ('alert(this.color)');
Thanks



Christian
__________________
Christian
Reply With Quote
  #2 (permalink)  
Old July 7th, 2006, 12:39 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

They two are roughly equivalent, but using new Function() causes the JavaScript interpreter to invoke another JavaScript interpreter to interpret the text you have passed in. Generally, the second method is frowned upon for this reason.

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

And you can create closures using both methods, right?

Thanks

Christian
Reply With Quote
  #4 (permalink)  
Old July 7th, 2006, 02:09 PM
AGS AGS is offline
Wrox Technical Editor
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2005
Location: Russia
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would say that the main difference is that the function created using Function constructor contains only global object in its scope chain. It can be illustrated by following example:
Code:
        function myFunc() {

            var myVar = "Test";

            var oCar = { color: "Red" };

            oCar.showColor = function () {
                alert(this.color);
                alert(typeof myVar);
            };

            oCar.showColor1 = new Function ('alert(this.color); alert(typeof myVar); ');           

            return oCar;        
        }

        var car = myFunc();

        car.showColor();
        car.showColor1();

Am I wrong, Nicholas?

Best regards,
Alexei

Reply With Quote
  #5 (permalink)  
Old July 7th, 2006, 02:17 PM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In other words, you can't create closures using the Function constructor, right?

Christian
Reply With Quote
  #6 (permalink)  
Old July 7th, 2006, 02:17 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

Yes, but it's harder to create using a string because you can't lay out your code in a logical way.

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

Nicholas,

Why then, on AGS example, the second function can not access myVar?

Thanks a lot guys for all your help,

Christian

Christian
Reply With Quote
  #8 (permalink)  
Old July 7th, 2006, 02:37 PM
AGS AGS is offline
Wrox Technical Editor
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2005
Location: Russia
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's your turn, Nicholas ;). The whole point of closures is in scope chain. And there is no one with the exception of global object.

Alexei

Reply With Quote
  #9 (permalink)  
Old July 11th, 2006, 04:23 PM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nicholas,

Is there a reason why on AGS' example the function defined with the Function Constructor cannot access the variable 'myVar'?

Christian
Reply With Quote
  #10 (permalink)  
Old July 11th, 2006, 05:09 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

Basically, creating a function creates a new scope. When you do it the old fashioned way, you can include objects from outside of that scope, so Alexei's showColor() function can access myVar. The showColor1() function, because it is created using a string, doesn't have that notion of a containing scope from which variables can be accessed. Within that code, myVar hasn't been defined yet, and since there is no other scope for it to check, it is considered undefined by the interpreter.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
Reply With Quote
Reply


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
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



All times are GMT -4. The time now is 04:52 AM.


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