Vic*_*Vic 20 javascript ecmascript-6 flowtype
有人可以解释之间的区别:
function foo(bar: ?string) {
console.log(bar);
}
Run Code Online (Sandbox Code Playgroud)
和:
function foo(bar?: string) {
console.log(bar);
}
Run Code Online (Sandbox Code Playgroud)
何时使用一个而不是另一个?
Jon*_*lms 29
基本上
bar: ?string
Run Code Online (Sandbox Code Playgroud)
接受字符串,null或void:
foo("test");
foo(null);
foo()
Run Code Online (Sandbox Code Playgroud)
而
bar?: string
Run Code Online (Sandbox Code Playgroud)
只接受字符串或空格:
foo("test");
foo();
Run Code Online (Sandbox Code Playgroud)
传递null而不是字符串有点无意义,它们之间没有真正的生命差异.
kin*_*ser 12
?string(也许型)表示bar的特性可以是string藏汉如null和void.
bar? 表示此属性是可选的.
更多信息:https://flow.org/en/docs/types/primitives/
| 归档时间: |
|
| 查看次数: |
5779 次 |
| 最近记录: |