Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers 3rd edition
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 3rd Edition by Nicholas C Zakas; ISBN: 978-1-1180-2669-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 3rd edition 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 Display Modes
  #1 (permalink)  
Old October 6th, 2015, 02:55 AM
Registered User
Points: 58, Level: 1
Points: 58, Level: 1 Points: 58, Level: 1 Points: 58, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem With Constructors

In chapter 6, the author explains that the constructor

function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
alert(this.name);
};
}

is logically equivalent to this:

function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = new Function("alert(this.name)"); //logical equivalent
}

But he very briefly mentions that "creating a function in this manner is different with regard to scope chains and identifier resolution".

I do not understand what he meant by that. Can anyone explain?

Thanks much!
Dyson
Reply With Quote
  #2 (permalink)  
Old November 10th, 2015, 01:25 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to lewisje
Lightbulb

It means that the Function constructor always creates functions in the global scope, while a function expression inside another function creates a function in that other function's inner scope; they're logically equivalent because alert is a global function and this is determined by execution context, not scope.

If the second example had created a variable named "alert" inside the outer function then the "alert" inside that Function constructor would still refer to the global alert.

If the first example had created a variable named "alert" inside the outer function then the "alert" inside that inner function expression would refer to the variable, and not the global alert.
Reply With Quote
  #3 (permalink)  
Old November 10th, 2015, 01:33 PM
Registered User
Points: 58, Level: 1
Points: 58, Level: 1 Points: 58, Level: 1 Points: 58, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much. I did not know that functions created with Function constructor always exist in the global scope.
Reply With Quote
Reply


Thread Tools
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
Constructors and Destructors mudgalbk Excel VBA 0 March 23rd, 2012 11:59 PM
Constructors madhuriguda C# 2 April 29th, 2008 08:11 AM
Constructors - instantiation brysora C# 2005 1 September 3rd, 2007 08:45 PM
Cascading Constructors rodmcleay General .NET 4 May 18th, 2007 03:57 AM
Static Constructors Ibn_Aziz C# 3 December 25th, 2003 03:07 AM



All times are GMT -4. The time now is 08:37 PM.


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