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

July 7th, 2006, 10:57 AM
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

July 7th, 2006, 12:39 PM
|
 |
Wrox Author
|
|
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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/
|

July 7th, 2006, 02:08 PM
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
And you can create closures using both methods, right?
Thanks
Christian
|

July 7th, 2006, 02:09 PM
|
Wrox Technical Editor
|
|
Join Date: May 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

July 7th, 2006, 02:17 PM
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
In other words, you can't create closures using the Function constructor, right?
Christian
|

July 7th, 2006, 02:17 PM
|
 |
Wrox Author
|
|
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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/
|

July 7th, 2006, 02:29 PM
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Nicholas,
Why then, on AGS example, the second function can not access myVar?
Thanks a lot guys for all your help,
Christian
Christian
|

July 7th, 2006, 02:37 PM
|
Wrox Technical Editor
|
|
Join Date: May 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

July 11th, 2006, 04:23 PM
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Nicholas,
Is there a reason why on AGS' example the function defined with the Function Constructor cannot access the variable 'myVar'?
Christian
|

July 11th, 2006, 05:09 PM
|
 |
Wrox Author
|
|
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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/
|
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 |
|
 |