什么代码能检测Array对象和Object对象 Array是包含在object对象中的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 function chkArr(obj){if(!obj.hasOwnProperty("length")){alert("不是数组");return false;}return true;}因为Array都有length属性,所以没有length属性的肯定不是数组,但有的object可能也有length属性,所以这个函数还不能完全判断。 修改一下,这样更准确一些:function chkArr(obj){if(obj.hasOwnProperty("length")){ if(obj.length!=0){ if(obj[0]){return true;}else{return false;} return true; }}return false} 建议使用constructor 或.length因为hasOwnProperty 要求 版本 5.5,而constructor 要求 版本 2<script>function chkArr(o){return typeof(o.length)!="undefined"}function isArr(o){return o.constructor==Array}var arr=[1,2,3,4]var obj={1:2,3:4}alert(chkArr(arr))alert(chkArr(obj))alert(isArr(arr))alert(isArr(obj))</script> 未必哦array,是数组,没有什么疑问吧?集合也有length啊。alert(document.links.length); <script>var aa = new Array();alert(aa instanceof Array); ///return true</script> var a = new Array();var b = new Object();alert(delete a.length); // 显示false; 因为这个属性不可删除alert(delete b.length); // 显示true; 因为根本无此属性 <script>var aa = new Array();alert(aa instanceof Array); ///return true</script>还是这个有道理。 很急的一个问题。 firefox 下的js提交不执行的bug 在线等ing…… 同时return true时才能submit怎么做。。 怎样在一个页面里控制其它页面的窗体对象? Jquery选择器问题 如何实现选项卡自动切换 一个层应用问题~~! 自己写的程序,有问题!在线等!急! 如何让浏览器中显示的word文档不能修改和拷贝 jQuery中location('')函数在Chrome中显示Uncaught ReferenceError: load is not defined错误 请问控一个窗口的长和宽是用哪个函数呀! showModalDialog的问题
if(!obj.hasOwnProperty("length")){
alert("不是数组");
return false;
}
return true;
}
因为Array都有length属性,所以没有length属性的肯定不是数组,但有的object可能也有length属性,所以这个函数还不能完全判断。
function chkArr(obj){
if(obj.hasOwnProperty("length")){
if(obj.length!=0){
if(obj[0]){return true;}else{return false;}
return true;
}
}
return false
}
因为hasOwnProperty 要求 版本 5.5,而constructor 要求 版本 2<script>
function chkArr(o){return typeof(o.length)!="undefined"}
function isArr(o){return o.constructor==Array}var arr=[1,2,3,4]
var obj={1:2,3:4}
alert(chkArr(arr))
alert(chkArr(obj))
alert(isArr(arr))
alert(isArr(obj))
</script>
var aa = new Array();
alert(aa instanceof Array); ///return true
</script>
var b = new Object();
alert(delete a.length); // 显示false; 因为这个属性不可删除
alert(delete b.length); // 显示true; 因为根本无此属性
var aa = new Array();
alert(aa instanceof Array); ///return true
</script>还是这个有道理。