Function bind() Method
Description
The bind() method calls a function with a given
this value.
The bind() method lets objects borrow methods from other objects.
Example
function fullName() {
let text = this.fName + " " + this.lName;
document.getElementById("demo").innerHTML = text;
}
// Create a person Object
const person = {
fName: "John",
lName: "Doe"
}
// Use fullName on person
let name = fullName.bind(person);
name();
Try it Yourself »
Syntax
method.bind(object, arg1,...,argN)
Parameters
| Parameter | Description |
| method | Required. The method to bind (the function to use). |
| object | Required. The object to bind the method to (to use the funtion on). |
| arg1,...,argN | Optional. The function arguments. |
Return Value
| Type | Description |
| Value | The result of the function. |
More Examples
Example
// Create person Object
const person = {
firstName:"John",
lastName: "Doe",
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
// Create member Object
const member = {
firstName:"Hege",
lastName: "Nilsen",
}
// Bind the fullName Method to the member Object
let fullName = person.fullName.bind(object);
Try it Yourself »
JavaScript Function Methods
Browser Support
bind() is an ECMAScript5 (ES5 2009) feature.
JavaScript 2009 is supported in all browsers since July 2013:
| Chrome 23 |
IE/Edge 11 |
Firefox 21 |
Safari 6 |
Opera 15 |
| Sep 2012 | Sep 2012 | Apr 2013 | Jul 2012 | Jul 2013 |