X
X
XiNull2021-06-27 18:57:25
Node.js
XiNull, 2021-06-27 18:57:25

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

1 answer(s)
P
Pavel Shvedov, 2021-06-27
@XiNull

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 question

Ask a Question

731 491 924 answers to any question