PROGRAMMING

 
REMEMBERS




Last update:   29-10-2021

What is binding

The bind() method takes an object as an first argument and creates a new function. The value of this in the function body will be the object passed as an argument. Function
 
let user1 = {name: "John"};
let user2 = {name: "Ana"};

function getName() {
    console.log(this.name);
}

getName(); // nothing

getName.bind(user1)(); // John
getName.bind(user2)(); // Ana
... 3 lines
 
Class Binding    (2/2)

Class binding

 
class User {
    getName() {
        return this.name;
    }
}

let user1 = {name: "John"};
let user2 = {name: "Ana"};

let user = new User();

console.log( user.getName() ); // undefiend
console.log( user.getName.bind(user1)() ); // John
console.log( user.getName.bind(user2)() ); // Ana
... 6 lines
 

Questions    
Arrays

        A B C D E F
🔔
1/1