// Have to assign “this” to a variable – not sure why can’t use directly
var self = this;

I’ve been struggling with this for days! The answer is that “this” is the context of the function/object definition (i.e. HTTPClient) at the time the constructor runs (i.e. at “new” time) but the context of the call when a callback occurs; i.e. when StateChangeCallBack() is called, this points inside some completely different object. Venkman (many Kudos to the Moz team for Venkman!) shows this quite nicely as you stumble through errors and exceptions.

This is where JavaScript closure comes in: A function “remembers” the context it was defined in, including all variables (except “this”, of course!) So when your callback runs, the “self” it refers to is the very “self” that was pointed at “this” at the time of creation of HTTPClient.

By convention, we make a private self parameter. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.