“this” and “that”: understanding execution context in javascript

JavaScript is the type of language that causes massive amounts of confusion in people who are used to more structured OOP languages like C# or Java. One of the aspects of the language that is not always clear is how the “this” keyword works. Unlike other OOP languages, the value of “this” is not always what one would expect. Let’s look at an example in C#:

namespace StevenHunt.Demo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            ThisDemo demo = new ThisDemo("test value");
            Console.WriteLine(demo.This());
        }
    }
    public class ThisDemo
    {
        private string val = "";
        public ThisDemo(string val)
        {
            this.val = val;
        }
        public string This()
        {
            return this.val;
        }
    }
}

As you can see in the program above, “this” in C# is always a reference to the current instance of the parent object. In fact, if there is no parent object to reference such as in the case of a static method, you cannot use the “this” keyword without receiving a compiler error. You have no control as to what the value of “this” is; the reference is automatically handled by the language itself. As a side note, “this” has an alternate use in C# when defining extension methods. Extension methods are outside of the scope of this article, but I wanted to mention them since they share the same keyword. Anyways, back to JavaScript…

In JavaScript, “this” is a valid keyword in any context, so what value could it possibly be referencing in the global context? Let’s start with the simplest case:

<script type="text/javascript">
    alert(this);
</script>

What does this example do? It generates an alert with the contents of “[object] [Window]”. What does this mean though? It means that “this” is referencing the window object, also accessible in your client-side JavaScript code with the variable “window”. If you’re running code in a global context, “this” will always reference the window object. Before we move to more advanced examples, consider the following code:

<script type="text/javascript">
    function setName(context, name) {
        if (!context.demoName)
            context.demoName = name;
    }

    function writeName(context) {
        alert(context.demoName);
    }
</script>

For the purpose of demonstrating “this” in the following examples, I’ve written two functions: setName() tags a javascript object with a friendly name if it doesn’t already have one, and writeName() writes that name to the screen. Here is the same example already given, but using these new functions:

<script type="text/javascript">
    setName(window, "window");
    writeName(this);
</script>

So, in this simple example we’re going to tag the name “window” to the window object, and then write out whatever the name of “this” is. If we were to run this we would get this output: “window”. Why did we get that? because if you’re writing code in a global context, “this” is a reference to the window object. What about functions?

<script type="text/javascript">
    setName(window, "window");
    var func1 = function() {
        setName(this, "func1");
        writeName(this);
    };
</script>

What if we then were to simply call the function like this:

func1();

Calling the function produces the following output: “window”. We’re calling the function from the global context, so “this” still references the window object. Let’s do something different and instantiate an instance of func1:

var f1 = new func1();

Running this line of code would output “func1” onto the screen. Because we’re treating the function as an object definition, the “this” keyword now references the new instance of the object being created. As we can see, “this” is set to the new instance of the object, but what about methods defined within the object itself?

<script type="text/javascript">
    setName(window, "window");
    var func2 = function() {
        setName(this, "func2");
        this.func3 = function() {
            setName(this, "func3");
            writeName(this);
        };
    };
</script>

Then, let’s say we do the following:

var f2 = new func2();
f2.func3();

Running that code would produce “func2”. Because we’re treating func3 as an instance method of an object defined by func2, the context of “this” will be the instance of “func2” referenced by the variable “f2”. If you really want to warp your mind and embrace the insanity that is JavaScript, what happens if we do this?

var f3 = new f2.func3();

First off, is that even valid? Why yes… yes it is. Once you can accept that, what’s the output? It would be “func3”, since “this” would be a reference to the instance of func3 we’re instantiating.

The key to dealing with “this” in javascript is understanding that the value it references can be different at runtime depending on how the code its used in is called. To  make matters even more complicated (yet ultimately very powerful), you can manually set the context of a function call yourself. Here’s an example with func1:

var mycontext = {};
setName(mycontext, "my context");
func1.call(mycontext);

Running this code would output “my context” to the screen. Using the call method of a function, you can set the context of the function as well as pass parameters. You can do the same thing with apply, the difference being whether you want to pass parameters as parameters of the function or as an array. This technique is used frequently in JavaScript libraries like jQuery to provide a reference to callback functions for events.

There’s one more example I would like to provide on this blog post before wrapping up. In accordance with the title, I’ve talked about “this”, however I haven’t talked about “that”. So, what is “that”? It’s just something I made up, but it makes handling the following scenario much easier. Let’s say you’re trying to do something like this:

<script type="text/javascript">
    setName(window, "window");
    var func2 = function() {
        setName(this, "func2");
        this.func3 = function() {
            setName(this, "func3");
            setTimeout(function() {
                writeName(this);
            }, 1000);
        };
    };
</script>

This example is identical to the previous func2/func3 example, with the exception of writeName being called inside of an anonymous function passed to setTimeout. For instance, something like this might be expected if you’re performing asynchronous actions and some part of your code needs to be in a callback that runs later. Anyways, what’s the output? It’s “window”, which may or may not be ideal for your functionality. But what happened to referencing the parent instance? Well, your code is now inside of an anonymous function which is called by setTimeout, which is an instance method of the window object. Therefore, “this” will refer to the window object. so, let’s go ahead and add “that” into the mix:

<script type="text/javascript">
    setName(window, "window");
    var func2 = function() {
        setName(this, "func2");
        this.func3 = function() {
            setName(this, "func3");
            var that = this;
            setTimeout(function() {
                writeName(that);
            }, 1000);
        };
    };
</script>

Now what? The output returns “func3”, hooray! But why? Basically, we copied the reference from “this” into a variable called “that”. Thanks to JavaScript closure, a function defined within another function has access to that parent function’s variables. That way when your callback runs, it has a valid reference to the instance of the object you expect it to have. Keep in mind that this only works if the instance itself is still around in memory, otherwise it won’t work for obvious reasons. I would also like to make a note that this technique may not be ideal if you plan on instantiating several thousand objects, as JavaScript will have to make a copy of the parent function’s variables for each instance.

So there you have it, “this” and “that”. Enjoy!