Default Binding of the this keyword in JavaScript

Default Binding of the this keyword in JavaScript

Understand the default binding of the this context in JavaScript

ยท

3 min read

Have you ever wondered what is your this context? In this article, we will learn about finding our Nemo i.e. the this context in the case of default binding.

But before that, letโ€™s have a primer on implicit and explicit bindings.

Implicit and Explicit Binding Primer

We can bind our this context either implicitly OR explicitly. You can read more about them in my What is this in JavaScript article.

In short, implicit binding happens when we call a this aware function with an object like this, where in the call site the eat function is invoked using the restaurant object:

var restuarant = {
  food: "๐Ÿ•๐Ÿ•๐Ÿ•",
  eat() {
    console.log(`I'm eating ${this.food}`);
  },
};

// Implicit binding
restuarant.eat(); // I'm eating ๐Ÿ•๐Ÿ•๐Ÿ•

Whereas, explicit binding happens either by using the new keyword OR by using a binding method on a function like call OR apply. The bind method internally uses apply method so it also does the explicit binding.

var restuarant = {
  food: "๐Ÿ•๐Ÿ•๐Ÿ•",
  eat() {
    console.log(`I'm eating ${this.food}`);
  },
};

// Using the call method
restuarant.eat.call({ food: "๐Ÿ”๐Ÿ”๐Ÿ”" }); // I'm eating ๐Ÿ”๐Ÿ”๐Ÿ”

// Using the apply method
restuarant.eat.apply({ food: "๐Ÿฅช๐Ÿฅช๐Ÿฅช" }); // I'm eating ๐Ÿฅช๐Ÿฅช๐Ÿฅช

// Using the bind method
var eat = restuarant.eat.bind({ food: "๐ŸŒฎ๐ŸŒฎ๐ŸŒฎ" });
eat(); // I'm eating ๐ŸŒฎ๐ŸŒฎ๐ŸŒฎ

Gif by Giphy [https://media.giphy.com/media/oQvpRPoB6BCyk/giphy.gif](https://media.giphy.com/media/oQvpRPoB6BCyk/giphy.gif)

Default Binding

When a function is invoked not using methods that cause implicit and explicit bindings, the fallback is default binding. In the non-strict mode, it will default to the global object. Whereas, in the strict mode, it will be undefined.

The reason for this being undefined in the strict mode is that itโ€™s a terrible idea to have this when you havenโ€™t bonded the function to any context.

var food = "๐Ÿ•๐Ÿ•๐Ÿ•";

function inNonStrictMode() {
  console.log(`I am eating ${this.food}`);
}

function inStrictMode() {
  "use strict";
  console.log(`I am eating ${this.food}`);
}

inNonStrictMode(); // I am eating ๐Ÿ•๐Ÿ•๐Ÿ•

inStrictMode(); // TypeError: Cannot read properties 
// of undefined (reading 'food')

Note that this.food for the inNonStrictMode function will be ๐Ÿ•๐Ÿ•๐Ÿ• when you execute this code in the browser where the global context will be the window object.

In Node.js, this.food for the inNonStrictMode function will be undefined. The reason for this is that Node.js wraps your script into a function and executes it. Something like this:

(function (exports, require, module, __filename, __dirname) {
  var food = "๐Ÿ•๐Ÿ•๐Ÿ•";

  function inNonStrictMode() {
    console.log(`I am eating ${this.food}`);
  }

  function inStrictMode() {
    "use strict";
    console.log(`I am eating ${this.food}`);
  }

  inNonStrictMode(); // I am eating ๐Ÿ•๐Ÿ•๐Ÿ•
  inStrictMode(); // TypeError: Cannot read properties of undefined (reading 'food')
})(exports, require, module, "file.js", "/dir/name");

Since a function has a this context, we use that context when we say this.food and since that this doesnโ€™t have a food property, we get undefined. Read more on this in this StackOverflow article.

This also makes one thing clear, you cannot figure out the this context by looking at either the function definition OR by the โ€œcall siteโ€. The manner in which the function is invoked i.e. the real call site will determine the this context.

Conclusion

Never use the this context when you havenโ€™t bonded your function with one, either implicitly OR explicitly. Itโ€™s a ๐Ÿ’ฉ big sin.

If you liked this post then do ๐Ÿ‘๐Ÿป clap and give your ๐ŸŽ™ feedback. Follow me to learn more about such topics.

Gif by Giphy [https://media.giphy.com/media/WoXxAq4QGsvCg/giphy.gif](https://media.giphy.com/media/WoXxAq4QGsvCg/giphy.gif)

ย