最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does "function(): any{" mean - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Apr 12, 2022 at 2:37 Alexander O'Mara 60.6k19 gold badges172 silver badges181 bronze badges asked May 30, 2015 at 4:17 Dave PileDave Pile 5,7744 gold badges37 silver badges51 bronze badges 4
  • Is this using a framework? – Cristian Cavalli Commented May 30, 2015 at 4:21
  • The snippet is part of reactjs but the particular line is plain javascript (I expect its some ES6 thing). Render is a member of an object – Dave Pile Commented May 30, 2015 at 4:23
  • 5 This isn't Javascript code. It is "flowtype", some sort of pre-processor. – Agamemnus Commented May 30, 2015 at 4:24
  • Perhaps any is it's return / accepted argument type? I'm kinda just looking at flow now. – Cristian Cavalli Commented May 30, 2015 at 4:26
Add a ment  | 

2 Answers 2

Reset to default 10

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.

Very simple and straightforward answer is function(): any will return any type of data.

It means you can return string boolean number or any type of data you want from that function.

发布评论

评论列表(0)

  1. 暂无评论