Pregunta

For some reason it is almost impossible to search for '=>' Can anyone explain exactly what it means and when it is used? Thank you!

¿Fue útil?

Solución

It's a lambda function and is used in TypeScript to maintain scope. In JavaScript this does not always refer to the scope you have defined it in, but is dependent on how the method is called. For example prototype.call may override what this refers to.

To combat this, an easy trick is to copy your scope in a variable (say self) and use this variable wherever you want to access the scope.

The lambada function in TypeScript does this for you, you're no longer required to do scope copying yourself.

Example:

class Example {
    aMethod = () => {
     // in here 'this' will always refer to the class scope
    }
}

Demo on Playground.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top