我有这个功能:
error = (error, modelState, modalHeader, modalBody): ng.IPromise<any> => {
// function code here
在某些情况下,第一个参数可以是字符串,例如:
"problem"
在其他情况下,它可能是这样的对象:
{
"ErrorMessage":"END_TEST - Invalid TestID, Unauthorized Access or TestStatus is not Started or Paused",
"ErrorNumber":50001
}
有没有办法可以检测它是字符串还是对象?
答案 0 :(得分:1)
typeof
operator是你的朋友:
error = (error, modelState, modalHeader, modalBody): ng.IPromise<any> => {
if (typeof error === 'string') {
//string
} else if (typeof error === 'object') {
//object
}
}
typeof
可评估为以下内容:string
,number
,object
,undefined
,boolean
,function
。< / p>