Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Exactly does => mean?

Tags:

typescript

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

like image 375
user1966361 Avatar asked Jun 02 '26 21:06

user1966361


1 Answers

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.

like image 184
thomaux Avatar answered Jun 06 '26 04:06

thomaux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!