有没有比typeof
更好的方法来获取JS中变量的类型?当你这样做的时候,效果很好:
> typeof 1
"number"
> typeof "hello"
"string"
但当你try 时,它是无用的:
> typeof [1,2]
"object"
>r = new RegExp(/./)
/./
> typeof r
"function"
我知道有instanceof
种,但这需要你事先知道类型.
> [1,2] instanceof Array
true
> r instanceof RegExp
true
有更好的办法吗?