您的位置:首页 > Web前端 > JavaScript

js判断变量类型(2种)

2008-12-17 23:09 399 查看
js判断变量类型 有2种方法

1.使用typeof

2.使用Variables.Constructor

Example:

<script type="text/javascript">

function fun(msg)

{

//使用typeof判断

if(typeof msg=="string")

{

alert("使用typeof判断:"+msg);

}

//使用constructor判断

if(msg.constructor==String)

{

alert("使用constructor判断"+msg);

}

}

fun("aa");

</script>

备注:
Type-Checking Variables

Variable typeof VariableVariable.constructor
{ an:“object” }objectObject
[ “an”,“array” ]object Array
function() {}function Function
“a string”stringString
55numberNumber
TruebooleanBoolean
new User()object User
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: