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

Coffeescript 如何检查变量是否为字符串类型,coffeescript,Coffeescript,我使用ajax获取数据,结果可以是结果数组,也可以是字符串语句,如“找不到结果”。我怎样才能知道我是否得到了任何结果? 我尝试过这种方法: if result == String do something 但它不起作用,就像 if typeof(result) == "string" do something 是否有其他函数可以帮助我获取变量的类型?或者我可以测试它的数组类型,检查结果是否为字符串也会非常有用: 这可以通过许多常见的方法来实现: 检查结果是否为数组: 您还可以

我使用ajax获取数据,结果可以是结果数组,也可以是字符串语句,如“找不到结果”。我怎样才能知道我是否得到了任何结果? 我尝试过这种方法:

if result == String
    do something
但它不起作用,就像

if typeof(result) == "string"
    do something
是否有其他函数可以帮助我获取变量的类型?或者我可以测试它的数组类型,检查结果是否为字符串也会非常有用

: 这可以通过许多常见的方法来实现:

检查结果是否为数组: 您还可以尝试使用本机的
Array.isArray
函数,然后返回到 与上面使用的类型检查类似的样式:

isArray = Array.isArray or (obj) -> toString.call(obj) == '[object Array]'
这行吗

if Object.prototype.toString.call(result) == '[object 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
条件的更多信息,请参阅

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