Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the notation () => mean and how to use it? [duplicate]

Tags:

javascript

I saw this piece of code in React, like

connect(mapStateToProps, {
    test: () => {return { type: 'TEST_ACTION' }}
})(Index);

but I failed to google any explanation. Probably the question is dumb, but I appreciate any help, maybe links to some existing explanations or examples.

like image 607
Battle_Slug Avatar asked Nov 23 '15 20:11

Battle_Slug


People also ask

What does => mean in math?

It stands for "implies that". For example, x=2⟹x2=4 - if x is 2, then it is obvious that x squared is 4; the symbol essentially shows a function here.

What does => mean in PHP?

It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.

What is the meaning of => in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .

What is the meaning of -> symbol?

The object operator, -> , is used in object scope to access methods and properties of an object. It's meaning is to say that what is on the right of the operator is a member of the object instantiated into the variable on the left side of the operator. Instantiated is the key term here.


1 Answers

That's an ES2015 (aka ES6) arrow function. It's a function expression that inherits this (and arguments, and a few other things) from the context where it's created. So basically:

test: function() { return { type: 'TEST_ACTION' }; }

...but using the newer syntax that would handle this differently, if it used this.

like image 110
T.J. Crowder Avatar answered Oct 07 '22 00:10

T.J. Crowder