Introduction
I wanted to write a little bit about JavaScript seeing as how I've been working with it so much lately. Specifically, I wanted to write about a common topic of confusion in multiple languages; the dreaded this. I'm only going to talk about this in terms of Java for now but many of these concepts are similar in other languages.A good place to start with this discussion is with execution contexts. The idea of execution contexts is embedded in how JavaScript is executed. The JavaScript run time environment keeps track of a these as a stack. As the script runs, the active context is on top. As the program leaves contexts by leaving functions or other scopes, this stack changes to always show the active window. This brings me to what defines a execution context in JavaScript.
Execution Context
When code runs in JavaScript, it executes within an environment. The environment that its running in is referred to as the execution context and has three distinct areas: global code, function code, and eval code. ECMAScript Standard definesthis
as a keyword that "evaluates to the value of the ThisBinding of the current execution context." There are a lot of resources and articles about scope online but for the purpose of this article I'm going to keep it simple. Global context refers to an area of execution that relates to the script in general meaning that when you're running code outside of any function. Function context is reached whenever the flow of execution enters a function's body. Eval context refers to the code to be executed inside of an internal eval function. I've separated some code below to highlight how these different contexts would look.
Global Context:
//Global Context int x = 1; int y = 1; int z = x + y;
Execution Context:
//Execution Context
function executionContextMethod(){
var hello = "Wassssup";
}
Eval Context:
function executionContextMethod(){
var hello = "Wassssup";
function EvalContext(){
//This is the eval context
}
There isn't anything going on here that is really complicated but the eval code deserves a closer look. Technically, where the comment is eval context but only if its called. This is an odd concept because we tend to think about programs only as they're written but in this scenario we have to think about the code in run time.
Since JavaScript is a single thread, meaning only 1 things can happen at once, the browser has to keep track of where it's current context is located.As a page loads, the script enters the global execution context. This means that its pointed at the global object. As it enters functions that are called, the context shifts an execution area and now refers to that function. This process is controlled by a stack that pushes and pops contexts depending on where its at.
The take away about execution contexts is that the focus of this depends solely on where the context is currently located at. I've put together a JSFiddle to demonstrate some of these concepts.
Conclusion
JavaScript may execute in a single thread (in most cases) but it is a very complex scripting language that has adopted many features of object oriented languages and will continue to get more complex and powerful. When thinking about this, always look at whats around it and think about the current execution context and you should be fine.