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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top