bind method in javascript

index.tsx
// You can bind a method to a specific object using the `bind()` method.
// Here is an example:

const person = {
    name: 'John',
    age: 30,
    greet: function() {
        console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
    }
};

const boundMethod = person.greet.bind(person);
boundMethod(); // Output: Hello, my name is John and I am 30 years old.
385 chars
14 lines

In this example, we bind the greet method of the person object to the person object itself using bind().

related categories

gistlibby LogSnag