Open this example in the interactive library
Source code
const person = {
name: "Ada",
hello() {
return this.name;
},
};
const hello = person.hello.bind(person);
console.log(hello());
Sample output
Ada

Preserve method context. A self-contained example using the JavaScript runtime.
Open this example in the interactive library
const person = {
name: "Ada",
hello() {
return this.name;
},
};
const hello = person.hello.bind(person);
console.log(hello());
Ada