Skip to content Skip to sidebar Skip to footer

How To Bind 'this' To An Object Arrow Function?

Let us suppose we have an object profile with properties name and getName method (arrow function). profile = { name: 'abcd', getName: () => { console.log(this.na

Solution 1:

Without changing it to a regular function, the only way to get to the name property from the inner function is through accessing the outer variable name, which is profile:

const profile = {
    name: 'abcd',
    getName: () => {
        console.log(profile.name);
    }
}

profile.getName();

Post a Comment for "How To Bind 'this' To An Object Arrow Function?"