您好,欢迎访问一九零五行业门户网

jquery怎么判断对象是否是数组

jquery判断对象是否是数组的方法:将需要判断的变量【$.isarray()】传入方法中,语法为【$.isarray( object ) / jquery.isarray( object )】。
jquery判断对象是否是数组的方法:
$.isarray()函数用于判断指定参数是否是一个数组。只需要将需要判断的变量传入方法中即可。
语法$.isarray( object ) / jquery.isarray( object )
参数 object 任意类型 需要进行判断的任意值。
返回值 true/false
<!doctype html><html><head><title>jq中的isarray方法分析</title><meta http-equiv="content-type" content="text/html; charset=utf-8" /></head><body><div class="warp"></div><script type="text/javascript">//下面是从jquery 1.9中抽出来的源码var class2type = {},core_tostring = class2type.tostring;//在firefox chrome ie9 ie10中都支持 array.isarray//使用方法 array.isarray([]) 返回 true//但是在ie7或更低版是不支持的var isarray = array.isarray || function( obj ) { return type(obj) === "array";};/*这一行也不能忽略 948行jquery.each("boolean number string function array date regexp object error".split(" "), function(i, name) { class2type[ "[object " + name + "]" ] = name.tolowercase();});一开始没关注这段代码,结果弄了半天 type([])都是返回 object那这段代码用js写出来如下*/var arr = "boolean number string function array date regexp object error".split(" ");for(var i in arr){ class2type[ "[object " + arr[i] + "]" ] = arr[i].tolowercase();};var type = function( obj ) { if ( obj == null ) { return string( obj ); } return typeof obj === "object" || typeof obj === "function" ? class2type[ core_tostring.call(obj) ] || "object" : typeof obj; // 相当于return (typeof obj === "object" || typeof obj === "function")?(class2type[ core_tostring.call(obj) ] || "object"): typeof obj;};//这样就可以了//后来在网上搜索了一下 发现了网上提供的方案是 return object.prototype.tostring.call(obj) === '[object array]';//详情查看 http://sofish.de/1591</script></body></html>
相关免费学习推荐:javascript(视频)
以上就是jquery怎么判断对象是否是数组的详细内容。
其它类似信息

推荐信息