JavaScript 怎么判断一个变量类型是否为数组?谢谢!

winder 2006-03-10 10:59:02
JavaScript 怎么判断一个变量类型是否为数组?谢谢!
...全文
355 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
winder 2006-03-10
  • 打赏
  • 举报
回复
谢谢!
mingxuan3000 2006-03-10
  • 打赏
  • 举报
回复
哦,我那个只能判断obiect和obiect array
clare2003 2006-03-10
  • 打赏
  • 举报
回复
var a="2eee";

调用
function func1(a){
if(a!=null){
if(a.length){
alert("array";
}else{
alert("is not array")
}
}
}
出来的结果是"array"
--------------------------




<script>
var a=new Array(1,2,3);
var b=2;
var c="2eee";

document.write(" The type of is " + typeof(a) );
document.write(" The type of is " + typeof(b) );
document.write(" The type of is " + typeof(c) );
//alert(a.length);

if(c!=null){
if(c.length){
alert("array");
}else{
alert("is not array")
}


}
</script>
mingxuan3000 2006-03-10
  • 打赏
  • 举报
回复

function func1(a){
if(a!=null){
if(a.length){
alert("array";
}else{
alert("is not array")
}
}
}
clare2003 2006-03-10
  • 打赏
  • 举报
回复
使用函数typeof
typeof 返回六种可能的值:“数字类型”、“字符串类型”、“布尔型”、“对象类型”、“函数类型”和“未定义类型”。
数组为认为一种object类型

<script language=javascript>
var a=new Array(1,2,3);
var b=2;
var c="2eee";

document.write(" The type of is " + typeof(a) );
document.write(" The type of is " + typeof(b) );
document.write(" The type of is " + typeof(c) );
alert(a.length);

</script>
hbhbhbhbhb1021 2006-03-10
  • 打赏
  • 举报
回复
<script language=javascript>
var a=new Array(1,2,3)
alert(a.constructor);
</script>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧