Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript 5th Edition
| 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 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 February 21st, 2017, 07: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;
}


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





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