gpt4 book ai didi

coffeescript - 如何检查变量是否为字符串类型

转载 作者:行者123 更新时间:2023-12-03 12:35:54 25 4
gpt4 key购买 nike

我正在用ajax获取数据,其结果可以是结果数组或字符串语句,例如“未找到结果”。我怎么知道我是否有结果?
我尝试过这种方法:

if result == String
do something

但它不起作用,就像
if typeof(result) == "string"
do something

还有其他功能可以帮助我获取变量的类型吗?或者也许我可以测试它的数组类型,这也将非常有帮助

最佳答案

使用typeof

doSomething(result) if typeof result is 'string'

请注意, typeof是运算符而不是函数,因此您无需编写 typeof(result)
您也可以这样做
doSomethingElse(result) if typeof result isnt 'string'

甚至
return if typeof result is 'string'
doSomething result
else
doSomethingElse result

有关 Coffeescript条件的更多信息,请参见 http://coffeescript.org/#conditionals

关于coffeescript - 如何检查变量是否为字符串类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20282997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com