The Wayback Machine - https://web.archive.org/web/20241002035335/https://www.geeksforgeeks.org/javascript-function-binding/
Open In App

JavaScript Function binding

Last Updated : 03 Jan, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In JavaScript function binding happens using the Bind() method. With this method, we can bind an object to a common function, so that the function gives different results when needed. otherwise, it gives the same result or gives an error while the code is executing. We use the Bind() method to call a function with ‘this’ value.

What is ‘this’?

‘this’ refers to the object it belongs to. The exact value of ‘this’ depends on how a function is called. In a method, ‘this’ represents the object that the method was called on, while in a regular function, it typically refers to the global object (in the case of browser environments, it’s the ‘window’ object).

Example 1: In this example, this keyword binds the name variable to the function. It is known as default binding. this keyword refers to geeks object.

javascript




let geeks = {
    name: "ABC",
    printFunc: function () {
        console.log(this.name);
    }
}
 
geeks.printFunc();


Output

ABC

Example 2: In this example, the binding of this is lost, so no output is produced.

javascript




let geeks = {
    name: "ABC",
    printFunc: function () {
        console.log(this.name);
    }
}
 
let printFunc2 = geeks.printFunc;
printFunc2();


Output

undefined

Example 3: In this example, we are using the bind() method in the previous example. The bind() method creates a new function where this keyword refers to the parameter in the parenthesis. This way the bind() method enables calling a function with a specified this value. 

javascript




let geeks = {
    name: "ABC",
    printFunc: function () {
        console.log(this.name);
    }
}
 
let printFunc2 = geeks.printFunc.bind(geeks);
//using bind()
// bind() takes the object "geeks" as parameter//
printFunc2();


Output

ABC

Example 4: In this example, there are 3 objects, and each time we call each object by using the bind()method. 

javascript




//object geeks1
let geeks1 = {
    name: "ABC",
    article: "C++"
}
//object geeks2
let geeks2 = {
    name: "CDE",
    article: "JAVA"
}
 
//object geeks3
let geeks3 = {
    name: "IJK",
    article: "C#"
}
 
function printVal() {
    console.log(this.name + " contributes about " +
                this.article + "<br>");
}
 
let printFunc2 = printVal.bind(geeks1);
//using bind()
// bind() takes the object "geeks1" as parameter//
printFunc2();
 
let printFunc3 = printVal.bind(geeks2);
printFunc3();
 
let printFunc4 = printVal.bind(geeks3);
printFunc4();
//uniquely defines each objects


Output

ABC contributes about C++<br>
CDE contributes about JAVA<br>
IJK contributes about C#<br>


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
Two Way Data Binding in javascript
In web development, keeping the user interface (UI) and the underlying data model in sync is crucial for a seamless user experience. Two-way data binding is a technique that automates this synchronization, reducing boilerplate code and simplifying development. What is Two-Way Data Binding?Imagine you have a text input field where users can enter th
4 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
Node.js This Binding
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 t
2 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
Vue.js Form Input Binding with Multiline text
Vue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications. Input Binding is used to sync and maintain the state of form input elements with the corresponding state in javascript. So, Vue.
2 min read
Types of data binding with template in Vue.js
In this article, we will see different ways to bind data with the template in Vue.js, along with understanding their implementation through the examples. When we wish to utilize our variables or data within the template, we generally use the mustache syntax(double curly braces). However, this isn't permitted in Vue for HTML elements. In order to us
8 min read
Vue.js Form Input Binding trim Modifier
Vue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications. Input Binding is used to sync and maintain the state of form input elements with the corresponding state in javascript. So Vue.j
2 min read
Vue.js Form Input Binding with Radio Option
Vue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications. Input Binding is used to sync and maintain the state of form input elements with the corresponding state in javascript. So Vue.j
3 min read
Vue.js Form Input Value Binding
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stag
5 min read
Vue.js Form Input Binding number Modifier
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stag
2 min read
Vue.js Form Input Binding lazy Modifier
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stag
3 min read
Angular PrimeNG Form SelectButton Value Binding Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the Form SelectButton Value Binding Component in Angular PrimeNG. The SelectButton Component is used to make
4 min read
Angular PrimeNG Form RadioButton Dynamic Values, Preselection, Value Binding and Disabled Option
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the Form RadioButton Dynamic Values, Preselection, Value Binding, and Disabled Option in Angular PrimeNG. The
3 min read
What is the difference between one-way data flow and two-way data binding in vue.js?
Vue.js is an open-source front-end JavaScript framework for building user interfaces and single-page applications. It follows Model-View-ViewModel (MVVM) architecture pattern. Vue.js has many in-built directives for DOM manipulation such as v-bind, v-on, v-model, etc. In this article, we will learn about the one-way data binding & two-way data
7 min read
How to binding inline styles in Vue.js ?
Vue.js is an open-source Model–View–ViewModel front-end JavaScript framework for building user interfaces and single-page applications. Every Web application needs HTML CSS styles for presenting a better User Interface. Vue.js allows you to dynamically render styling with data model structure. Usually, we can bind the styles using <style> tag
4 min read
How to avoid the need for binding in ReactJS ?
In ReactJs, when we are working with class-based components and want to access this inside a class method. This will need to bind it. Binding this allows it to access the state and setstate inside the class. Prerequisites:React JSReact JS Class ComponentsReact JS bind() methodApproach to avoid binding in React JS:To avoid the need for binding we ha
3 min read
What is the alternative of binding this in the constructor ?
The concept of binding this keyword is not reacted specifically but rather Javascript-specific. We need to bind this keyword because the value of this keyword in a simple function is undefined. For example, consider the code below. If this is a click handler in a functional component then the output will be undefined ( only in strict-mode, in non-s
4 min read
How to achieve Two-Way Data Binding in Angular with ngModel ?
Two-way Data Binding in Angular allows you to automatically synchronize data between a component class property and an input element (e.g., an input field) in your template. To achieve this binding, it's typically used with Angular [(ngModel)] directive. This is basically the combination of the Property Binding & the Event Binding, which helps
3 min read
Angular PrimeNG Form Listbox Value Binding Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn how to use the ListBox Component in Angular PrimeNG. ListBox component is used to make a list component from which we c
4 min read
Angular PrimeNG Form Password Model Binding
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the Password Component in Angular PrimeNG. The Password Component is used to represent the strength indicator
3 min read
Angular PrimeNG Form Inputtext Model Binding
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the InputText Component in Angular PrimeNG. Let’s learn about the properties, styling & syntaxes that wil
3 min read
Interpolation vs. Property Binding in Angular
Angular, a powerful front-end framework, offers various techniques to dynamically update and display data in your applications. Two commonly used methods for this purpose are interpolation and property binding. In this article, we will learn about interpolation and property binding and also see the key differences between them. Table of Content Int
4 min read