Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript 5th Edition
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning JavaScript 5th Edition
This is the forum to discuss the Wrox book Beginning JavaScript, 5th Edition by Jeremy McPeak; 978-1-118-90333-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 5th 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 February 21st, 2017, 06:51 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2017
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Confused regarding scope and this keyword described in CH14

In chapter 14 while writing the HTTP Request Constructor the book mentions that in order to get around scope it's using tempRequest as a pointer to point to the specific object.

I'm still relatively new to Javascript and I'm trying to get a grasp on scope and how the this keyword works. From everything I read, in this case it does not make sense to do this. So I tested and changed the inside condition to this.readystate instead of the tempRequest and added a console.log to verify. The function works just the same and the object that is referenced by this is the XMLHttpRequest object.

I can see where this would needed especially if you're calling functions that are called by other objects but in this case the reqReadyStateChange() function is still called by the XMLHttpRequest object.

The book makes this further confusing by saying "...because this points to the reqReadystatechange() function indeed of the HttpRequest object."

From what I gather this property will never be set to a function. It will be set to the object that is making the call.

Please tell me I'm not losing my mind...

From Book:
HTML Code:
function HttpRequest(url, callback) {
    this.request = new XMLHttpRequest();
    this.request.open("GET", url);

    var tempRequest = this.request; // <---- I DONT SEE THE NEED FOR THIS
    
    function reqReadyStateChange() {
        if (tempRequest.readyState == 4) {
            if (tempRequest.status == 200) {
                callback(tempRequest.responseText);
            } else {
                alert("An error occurred trying to contact the server.");
            }
        }
    }
    
    this.request.onreadystatechange = reqReadyStateChange;
}

HttpRequest.prototype.send = function () {
    this.request.send(null);
};
Tested by me: (works exactly the same)
The added console.log shows that the same object is set as this when the function is called.
HTML Code:
	function reqReadyStateChange() {
		console.log(this); // <-- THIS POINTS TO THE XMLHTTPREQUEST OBJECT
		if (this.readyState == 4) {
			if (this.status == 200) {
				callback(this.responseText);
			} else {
				alert("An error occurred trying to contact the server.");
			}
		}
	}

	this.request.onreadystatechange = reqReadyStateChange;
}
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
Ch14 TextEditor Ray_D BOOK: Ivor Horton's Beginning Visual C++ 2010 1 November 20th, 2011 02:26 PM
Ch14, Try it out pg505-pg511 TeaDrinkingGeek BOOK: Beginning ASP.NET 4 : in C# and VB 2 March 19th, 2011 06:07 AM
Ch14, Try out pg493 TeaDrinkingGeek BOOK: Beginning ASP.NET 4 : in C# and VB 3 March 18th, 2011 09:25 AM
My VB Code for Ch14 PatFrank BOOK: Beginning ASP.NET 2.0 and Databases 8 April 17th, 2007 02:34 PM
Ch14 - timesheets with one row geoffers BOOK: Beginning VB.NET Databases 0 August 24th, 2005 08:48 AM



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


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