Binding of ‘this’ in Javascript

In JavaScript, the ‘this’ keyword plays a crucial role in determining the context within which a function is executed. Understanding and effectively managing ‘this’ is important for writing clean and maintainable code in JS. In this blog, we will dive deep into the concept of ‘this’ and explore various techniques for binding it in different scenarios. Here, We are going to explain- what are the ways of javascript this binding. We will start from basics which means you first need to understand what is binding in Javascript. By the end, you will have a clear understanding of how ‘this’ works and be equipped with the knowledge to control its behavior in your JavaScript applications.

What is Binding?

Binding in JavaScript (Javascript bind) describes the process of connecting a value to a specific variable or function. It determines the scope and accessibility of variables and the behavior of functions. In simple terms, It determines how the ‘this’ keyword behaves within a function and allows you to control the value of ‘this’ when the function is called. Binding is important because it influences how functions access and interact with variables, properties, and methods within different contexts.

What is ‘this’ keyword in JS?

In JavaScript, the ‘this’ keyword plays a crucial role in determining the context within which a function is executed. it refers to the object that owns or calls the currently executing function. Its value is dynamically determined at runtime based on the invocation context. Below we have mentioned the ways of ‘this’ binding in javascript.

Ways of Binding ‘this’ Keyword in JS

There are different ways of binding ‘this’ in js.

1. Implicit Binding

The most common way for ‘this’ keyword to be bound is implicity. Implicit binding is the best method of binding this in javascript. This happens when a function is called a method of an object. In this case, ‘this’ will refer to the object that the function is called on. and it is automatically done by the JS engine.

Here’s an example:

In the above example, ‘Person’ is the context, and when ‘sayName()’  function is called on the ‘Person’ object, ‘this’ refers to the whole object that is the ‘Person’ object. 

2. Explicit binding

Explicit binding is yet another way of binding this in javascript. The ‘this’ keyword can also be bound explicitly. If You want to explicitly specify the ‘this’ value for a function,  then this technique is used. It can be done using the call(), apply(), or bind() methods.

1. The call() method takes a list of arguments, and it binds ‘this’ to the first argument.

           Here’s an example:

In the above example, call() is used to invoke the ‘fun()’ function with the ‘this’ value set to the ‘person’ object. This allows us to access the ‘name’ property of the ‘person’ object within the ‘fun’ function.

We can also pass additional arguments to the function using call() :

Here, the second argument of ‘call()’ is used to pass the message argument to the ‘call()’ function.

2. The apply() method is similar to the call() method, but it takes an array of arguments instead of a list of arguments.

Here’s an example:

In the above example, apply() is used to invoke the ‘fun()’ function with ‘this’ value set to the ‘person’ object. The ‘message’ array is passed as the second argument, which is then spread into individual arguments for the ‘fun()’ function.

3. The javascript bind() methods creates a new function with a bound ‘this’ value and any additional arguments provided. The original function is not modified.

  Here’s an example:

In  the above example, bind() is used to create a new function ‘greetPerson’ with ‘this’ value set to the ‘person’ object. The ‘greetPerson’ function can be invoked separately. We can also present arguments using bind().

3. Using ‘new’ Keyword

When a function is used with the ‘new’ keyword, it is called as a constructor function and ‘this’ keyword inside the function is bound to the newly created object. This means that any references to ‘this’ inside the function will refer to the new object.

Here’s an example:

In the above example, The ‘this’ keyword inside ‘car’ function will refer to the ‘myCar’ object, so the make, model, and year properties will be set on the myCar object.

4. Default Binding

in javaScript, when a function is called without any special syntax, ‘this’ refers to the global object. In a browser, the global object is window.

Here’s an example:

In the above example, ’this’ keyword is not resolved with any of the bindings, implicit , explicit, or  ‘new’, then the ‘this’ is bound to the window(global) object.

Conclusion

Understanding the concept of binding and knowing how to control the behavior of ‘this’ is crucial in JavaScript to ensure the proper functioning and avoid unexpected results when working with functions and objects. Above, we have explained the binding in Javascript of “this” keyword. Here are the highlights of the same:

  • In the case of implicit binding, this binds to the object adjacent to the dot(.) operator while invoking the method.
  • In the case of explicit binding, we can call a function with an object when the function is outside of the execution context of the object. The methods call(), apply(), and bind() play a big role here.
  • When a function is invoked with the ‘new’ keyword, ‘this’ keyword inside the function binds to the new object being constructed.
  • When the ‘this’ keyword is not resolved with any of the bindings, implicit, explicit, or new, then this is bound to the window(global) object. In JavaScript’s strict mode, this will be undefined.

Recent Post

  • Future Trends in AI Chatbots: What to Expect in the Next Decade

    Artificial Intelligence (AI) chatbots have become indispensable across industries. The absolute conversational capabilities of AI chatbots are enhancing customer engagement, streamlining operations, and transforming how businesses interact with users. As technology evolves, the future of AI chatbots holds revolutionary advancements that will redefine their capabilities. So, let’s start with exploring the AI chatbot trends: Future […]

  • Linguistics and NLP: Enhancing AI Chatbots for Multilingual Support

    In today’s interconnected world, businesses and individuals often communicate across linguistic boundaries. The growing need for seamless communication has driven significant advancements in artificial intelligence (AI), particularly in natural language processing (NLP) and linguistics. AI chatbots with multilingual support, are revolutionizing global customer engagement and service delivery. This blog explores how linguistics and NLP are […]

  • How Reinforcement Learning is Shaping the Next Generation of AI Chatbots?

    AI chatbots are no longer just about answering “What are your working hours?” or guiding users through FAQs. They’re becoming conversation partners, problem solvers and even reporting managers and sales agents. What’s driving this transformation? Enter Reinforcement Learning (RL)—a type of machine learning that’s changing the way chatbots think, learn, and respond. At Codalien Technologies, […]

  • AI Chatbots for Sales Team Automation: The Critical Role of AI Sales Assistants in Automating Your Sales Team

    Sales teams are the heart of any successful business, but managing them effectively can often feel like trying to juggle flaming swords. The constant pressure to generate leads, maintain relationships, and close deals leaves your team overwhelmed, spending more time on administrative tasks than actual selling. Here’s where AI-powered sales assistants step in to completely […]

  • Transforming HR with AI Assistants: The Comprehensive Guide

    The role of Human Resources (HR) is critical for the smooth functioning of any organization, from handling administrative tasks to shaping workplace culture and driving strategic decisions. However, traditional methods often fall short of meeting the demands of a modern, dynamic workforce. This is where our Human Resource AI assistants enter —a game-changing tool that […]

  • How Conversational AI Chatbots Improve Conversion Rates in E-Commerce?

    The digital shopping experience has evolved, with Conversational AI Chatbots revolutionizing customer interactions in e-commerce. These AI-powered systems offer personalized, real-time communication with customers, streamlining the buying process and increasing conversion rates. But how do Conversational AI Chatbots improve e-commerce conversion rates, and what are the real benefits for customers? In this blog, we’ll break […]

Click to Copy