1

Can someone explain why this works:

Object.defineProperty(Array.prototype, 'getRandom', {
    value: function(){return this[randomInt(0, this.length)]}
})

But why this doesn't works?

Object.defineProperty(Array.prototype, 'getRandom', {
    value: () => this[randomInt(0, this.length)]
})
Bjop
  • 152
  • 1
  • 11
  • 3
    The `this` keyword gives a different value in arrow functions compared to regular functions. [More info](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – James Mar 29 '22 at 12:49

0 Answers0