Array对象的判定
对象 Array 判定
2023-09-11 14:21:53 时间
/* 关于JS对象类型的判断,最复杂的在于RegExp和Array了,判定RegExp的情形不较少,而Array就比较多了,下面就是判断Array的方法 */ //方法一:利用instanceof来判断对象是不是Array的实例 function isArray(arr){ return arr instanceof Array; } //方法二:利用constructor来判断 function isArray(arr) { return !!arr && arr.constructor==Array; } //方法三:利用constructor和array的内置属性(实例方法)来判断一个对象是否是Array类型 function isArray(arr) { return arr && typeof arr==="object" && 'splice' in arr && 'join' in arr; } //方法四:通过sort方法的类型判断arr是不是Array对象的实例 function isArray(arr) { return typeof arr.sort==="function"; } //方法五:通过Array.prototype.toString.call()方法来判断对象 function isArray(o) { try { Array.prototype.toString.call(o); return true; } catch (e) {} return false; } //方法六:通过typeof和数组的length属性来判断 function isArray(o) { if(o && typeof o=="object" && typeof o.length=="number" && isFinite(o.length)) { //通过length属性是否符合原生数组的length的特性来进行双重判定 var _originalLength=o.length; o[o.length]="_test_"; var _newLength=o.length; o.length=_originalLength; return _newLength==o.length+1; } return false; } //方法七: function isArray(array) { var result=false; try { new array.constructor(Math.pow(2,32)); } catch(e) { result=/Array/.test(e.message); } return result; }
相关文章
- 《Effective Java》第2章 对所有对象都通用的方法
- ActiveAndroid:活性记录的风格ORM(对象关系映射)
- node 父子进程传递对象
- JavaScript 数组(Array)对象
- Scala入门到精通——第六节:类和对象(一)
- Array对象的方法实现(1)----Array.prototype.push和Array.prototype.concat(实现常规参数的功能)
- Array对象的方法实现(2)----Array.prototype.every和Array.prototype.fill(实现常规参数的功能)
- ASP里Request对象的探讨
- 对象模型图(OMD)阅读指南
- 编辑的自己闪烁对象
- java怎样将一组对象传入Oracle存储过程
- js hook array对象的push方法
- VB编程:利用CallByName函数对对象进行操作-23_彭世瑜_新浪博客
- 【软件架构思想系列】从伟人《矛盾论》中悟到的软件架构思想真谛:“对象”即事物,“函数”即运动变化...
- 9.5.4 Row对象
- AJAX - 创建 XMLHttpRequest 对象