1

I'm using the mongoose library in node and ran into a problem. When using pre hook for saving i can't get the reference of the document as 'this' using arrow functions.

I was suggested of using 'function' statement but there is a style guide I must follow. Is there a way to get the object intended for 'this' while using an arrow function?

Jake Ortiz
  • 503
  • 9
  • 20

1 Answers1

5

An arrow function applies lexical binding to the this keyword, effectively making it a constant that equals whatever this is equal at the outer scope when the arrow function is evaluated. Not even using function.call(), function.apply() or function.bind() can override that value.

You should inform whoever it is that defined and enforces the style guide that their rules are broken and can't be followed in real scenarios. Besides, arrow functions don't replace "standard" functions, they add a new language feature.

Amit
  • 45,440
  • 9
  • 78
  • 110