Hey, I’m not a big fan of wordy Medium articles, so here is my abridged version of determining the context of ‘this’.
Golden rule: Look to how the function is invoked at call time.
There are five bindings that help us determine the context of this: Global, Immediately-invoked function expressions, Method invocation, Constructor mode, and .call(), .apply(), and .bind().
Global: When ‘this’ is referenced in the global scope, ‘this’ will refer to the Global Window Object. Pretty simple!

Immediately-invoked function expression: When a function is called freely in the global scope, ‘this’ will once again refer to to the Global Window Object. The keyword here is “global”, meaning the object ‘this’ points to in this scope will always be the Global Window Object. It’s all in the name!


Method Invocation: When a function is assigned to a property of an object, ‘this’ will always refer to the object on the left of the dot. If we think back to basic rules for accessing objects with dot notation, we place the name of the object to the left of the dot, and we place the key on the right. The same concept applies here in that when the function is being called at runtime, we need only look to the left of the dot to determine the object upon which we are calling the function.

“Katherine Pierce” is printed to the console when the fullName function is invoked.
Constructor mode: The ‘new’ keyword added to Javascript for ES6 creates a new object by setting the prototype, executing the constructor function with ‘this’ (linking the object to the new object), and finally returning the new object. Therefore, ‘this’ will always point to the newly created object.

Now the new elena object is printed to the console!
Using .call(), .apply(), .bind().
- .call — this method is used to call an object’s method and can pass another object as an argument.
- .apply — this method called similarly to .call() only it passes any additional arguments in an array. It also uses an object’s method and can pass another object as an argument.
Let’s look at an example.


In the immediately-invoked function expression example above, when I console.log(this) in the createVampire function, the Global Window Object was logged to the console. If I were to console.log(this) again in the details or moreDetails functions, we would instead see our tvShow object.
Now, let’s finally talk about .bind — this method binds a function to an object. This method essentially glues a reference to ‘this’ so you always know to which object ‘this’ refers.
Our new vampire we created not too long ago, Elena, is thirsty! She doesn’t like human blood, so she’s going to go hunting in the woods…

Hope these examples and explanations were helpful! Creating examples using personal interests helps me to have fun with code that I am playing around with when learning new concepts, hence The Vampire Diaries references! :) Thanks for reading, there will be more… xoxo Gossip Girl