Wrox Programmer Forums
| 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 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
  #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
  #2 (permalink)  
Old November 10th, 2015, 02: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.
  #3 (permalink)  
Old November 10th, 2015, 02: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.


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 04:07 AM





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