admin管理员组文章数量:1332345
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
2 Answers
Reset to default 10That'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.
本文标签: javascriptWhat does quotfunction() anyquot meanStack Overflow
版权声明:本文标题:javascript - What does "function(): any{" mean - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742326931a2453912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论