Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "function(): any{" mean

Tags:

javascript

I saw this snippet here:

render: function(): any {
  var thread = this.state.thread;
  var name = thread ? thread.name : "";
  var messageListItems = this.state.messages.map(getMessageListItem);
  return (
    <div className="message-section">
    <h3 className="message-thread-heading">{name}</h3>
// ...

What does the function(): any{ part in the first line mean?

Apologies if this has been asked before, but it's really hard to search this, particularly when you don't know what it's called.

like image 322
Dave Pile Avatar asked May 30 '15 04:05

Dave Pile


People also ask

What does function () do in JavaScript?

Points to Remember : JavaScript a function allows you to define a block of code, give it a name and then execute it as many times as you want. A function can be defined using function keyword and can be executed using () operator.

What is of () in JavaScript?

of() The Array. of() method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments. The difference between Array. of() and the Array constructor is in the handling of integer arguments: Array.

What is $( function () in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action()

What does is an function do?

A function is simply a “chunk” of code that you can use over and over again, rather than writing it out multiple times. Functions enable programmers to break down or decompose a problem into smaller chunks, each of which performs a particular task.


1 Answers

That's not a part of JavaScript, it's an extra feature added by Flow, a JavaScript preprocessor. TypeScript also has a similar feature.

Essentially, Flow adds a type-checking feature, and to use it you add type-hinting to symbols. In this case, : any is a type hint for the render method, meaning the method could return any type.

Excerpt from the type annotations docs for any:

any is a special type annotation that represents the universal dynamic type. any can flow to any other type, and vice-versa. any is basically the "get out of my way, I know what I am doing" annotation. Use it when Flow is getting in your way, but you know your program is correct.


A fun little side note, there was a proposed feature in the now-abandoned ES4 draft for type hinting that was very similar to this. As far as I know, it was only ever implemented in the ES-derived ActionScript 3.

like image 131
Alexander O'Mara Avatar answered Oct 15 '22 13:10

Alexander O'Mara