Answer the question
In order to leave comments, you need to log in
How to pass object context to arrow function in object?
Hello.
Is it possible to somehow pass the object's context to an arrow function in the object?
const person = {
name: 'Admin',
delayLog: () => {
const self = this
setTimeout(function() {
console.log("name: " + self.name)
}, 500)
}
}
person.delayLog() // name: undefined
Answer the question
In order to leave comments, you need to log in
const person = {
name: 'Admin',
delayLog() {
setTimeout(() => {
console.log("name: " + this.name)
}, 500)
}
}
person.delayLog() // name: Admin
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question