Is there a better way to get the type of a variable in JS than typeof
?
(有没有比typeof
更好的方法来获取JS中的变量类型?)
It works fine when you do:(当您这样做时,它可以正常工作:)
> typeof 1
"number"
> typeof "hello"
"string"
But it's useless when you try:
(但是当您尝试以下操作时,它是没有用的:)
> typeof [1,2]
"object"
>r = new RegExp(/./)
/./
> typeof r
"function"
I know of instanceof
, but this requires you to know the type beforehand.
(我知道instanceof
,但这需要您事先知道类型。)
> [1,2] instanceof Array
true
> r instanceof RegExp
true
Is there a better way?
(有没有更好的办法?)
ask by Aillyn translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…