Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the colon mean in Javascript after function?

I saw the Facebook F8 app code there is a ":" after the function

function setup(): React.Component {
  ...
}

What does this mean?

Inheritance?

like image 273
Jo_cn Avatar asked Apr 20 '16 10:04

Jo_cn


People also ask

What does the colon mean in JS?

The colon symbol ( : ) is generally used by JavaScript as a delimiter between key/value pair in an object data type. For example, you may initialize an object named car with key values like brand and color as follows: let car = { brand: "Toyota", color: "red", };

What is () after function in JavaScript?

Show activity on this post. (function () {}) creates an anonymous function. Adding the () to the end calls the function that was just created. In the case of this particular function, the anonymous function returns several properties to the Browser object.

What Does a colon do in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.

What goes in the parentheses after a function JavaScript?

In JavaScript, the functions wrapped with parenthesis are called “Immediately Invoked Function Expressions" or "Self Executing Functions. The purpose of wrapping is to namespace and control the visibility of member functions. It wraps code inside a function scope and decrease clashing with other libraries.


2 Answers

Usually it's flowtype annotation and in this case means that setup() returns a React.Component. Or it could be TypeScript as well, can't make sophisticated guess in this particular case.

like image 162
Samuli Hakoniemi Avatar answered Oct 17 '22 05:10

Samuli Hakoniemi


I think that is the return type of the function setup(), if you analyze different examples you will see that syntax in method parameters too

function user(state: State = initialState, action: Action): State {
  // some code
  return state;
}

edit: is flow, a static type checker from facebook: https://flow.org/

like image 3
avalla Avatar answered Oct 17 '22 05:10

avalla