0
let data2 = {
    address: 'New Delhi ',
    pin: '201206',
    details: () => {
        return this.address + " " + this.pin
    }
}

Here, when I call data2.details(), so output is undefined, but when using the es5 pattern,

let data2 = {
    address: 'New Delhi ',
    pin: '201206',
    details: function () {
        return this.address + " " + this.pin
    }
}

it’s working properly?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Shivam Gupta
  • 533
  • 4
  • 9
  • 2
    Are you aware of the differences between arrow functions and regular functions? You should look them up before find-and-replacing `function ()` to `() =>`. – Ry- Aug 16 '17 at 07:53
  • i study about differences but can u please explain how will this code work – Shivam Gupta Aug 16 '17 at 07:59
  • Arrow functions do not bind a `this` value, for instance. It is all explained in the question yours is marked as a duplicate of. – Phylogenesis Aug 16 '17 at 08:04
  • Thanks solved this by - let data2 = { address: 'New Delhi ', pin: '201206', details () { return this.address + " " + this.pin } } so i didn't need a arrow function => if declaring method inside method ! – Shivam Gupta Jan 02 '19 at 06:39

0 Answers0