The Wayback Machine - https://web.archive.org/web/20240804041038/https://www.geeksforgeeks.org/node-js-this-binding/
Open In App

Node.js This Binding

Last Updated : 08 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Arrow functions don’t bind their own ‘this’ value. Instead, the ‘this’ value of the scope in which it was defined is accessible. This makes arrow functions bad candidates for methods, as this won’t be a reference to the object the method is defined on.

For methods, ES6 provides a new method definition syntax. You can see this in the definition of the printGuestList method below. That function is a standard function, just with a shorthand syntax which allows for the removal of the colon and the function keyword.

Because arrow functions don’t bind this, they work well for everything except methods. As shown below, the arrow function passed to forEach is able to access this.name correctly, as it’s defined as an arrow function and doesn’t have this binding of its own. That code wouldn’t work if you swapped out the arrow function for a standard function.

Create a folder and add a file for example index.js. To run this file you need to run the following command.

node index.js

Filename: index.js




// 'This' in Arrow function
const eventOne = {
    name: 'Birthday Party',
    guestList: ['Gourav', 'Vijay'],
    printGuestList() {
        console.log('Guest list for ' + this.name);
        this.guestList.forEach((guest) => {
            console.log(guest + ' is attending ' + this.name)
        });
    }
}
  
// 'This' in normal function
const eventTwo = {
    name: 'Birthday Party',
    guestList: ['Gourav', 'Vijay'],
    printGuestList() {
        console.log('Guest list for ' + this.name);
        this.guestList.forEach(function (guest) {
            console.log(guest + ' is attending ' + this.name)
        });
    }
}
  
eventOne.printGuestList();
console.log('---------------------');
eventTwo.printGuestList();


Steps to run the program:

  • Run index.js file using the following command:
    node index.js

    Output of above command

So this is how ‘this’ binding works on arrow functions and normal functions.



Similar Reads

Difference between One-way Binding and Two-way Binding
In this article, we will learn the concept of data binding in Angular. We will also explore its types & examine the differences between one-way binding and two-way binding in angular. Data binding is a way to synchronise the data between the model and view components automatically. AngularJS implements data-binding that treats the model as the
3 min read
AngularJS Data Binding
In this article, we will see the Data Binding in AngularJS, along with understanding the various types of Data Binding available with their implementations. Angular provides a function Data Binding which helps us to have an almost real-time reflection of the input given by the user i.e. it creates a connection between Model and View. Data Binding i
4 min read
ReactJS | Binding Event Handlers
In ReactJS, we need to bind events so that the this keyword would not return an "undefined". In this article, we are going to see the different ways in which we can bind event handlers in ReactJS. First, let's make a new class component and name it Binding.js. Make a simple class component with a simple message state and render a simple button as s
3 min read
Style Binding in Angular 8
It is very easy to give the CSS styles to HTML elements using style binding in Angular 8. Style binding is used to set a style of a view element. We can set the inline styles of an HTML element using the style binding in angular. You can also add styles conditionally to an element, hence creating a dynamically styled element. Syntax: <element [s
1 min read
Event Binding in Angular 8
In Angular 8, event binding is used to handle the events raised by the user actions like button click, mouse movement, keystrokes, etc. When the DOM event happens at an element(e.g. click, keydown, keyup), it calls the specified method in the particular component. Using Event Binding we can bind data from DOM to the component and hence can use that
2 min read
Property binding in angular 8
Property Binding is a one-way data-binding technique. In property binding, we bind a property of a DOM element to a field which is a defined property in our component TypeScript code. Actually, Angular internally converts string interpolation into property binding. In this, we bind the property of a defined element to an HTML DOM element. Syntax:
2 min read
How to avoid binding by using arrow functions in callbacks in ReactJS?
In React class-based components when we use event handler callbacks, it is very important to give special attention to the 'this' keyword. In these cases the context this is undefined when the callback function actually gets invoked that's why we have to bind the context of this. Now if binding all the methods of each class is very annoying. Instea
2 min read
Class Binding in Angular 8
Class binding in Angular makes it very easy to set the class property of a view element. We can set or remove the CSS class names from an element's class attribute with the help of class binding. We bind a class of a DOM element to a field that is a defined property in our Typescript Code. Its syntax is like that of property binding. Syntax: <el
2 min read
Vue.js Two Way Binding Model
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. The v-model directive makes two-wa
2 min read
Two-way Data Binding in AngularJS
In this article, we will see the Data Binding, along with understanding how the flow of code is from a Typescript file to an HTML file & vice-versa through their implementation. In AngularJS, Data Binding refers to the synchronization between the model and view. In Two-way data binding, the flow of data is bidirectional i.e. when the data in th
3 min read
three90RightbarBannerImg