Created
August 11, 2013 09:48
-
-
Save 6174/6204199 to your computer and use it in GitHub Desktop.
isArrayLike
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isArrayLike(obj) { | |
if (obj && typeof obj === "object") { | |
var n = obj.length | |
if (+n === n && !(n % 1) && n >= 0) {//检测length属性是否为非负整数 | |
try { | |
if ({}.propertyIsEnumerable.call(obj, 'length') === false) {//如果是原生对象 | |
return Array.isArray(obj) || /^\s?function/.test(obj.item || obj.callee) | |
} | |
return true; | |
} catch (e) {//IE的NodeList直接抛错 | |
return true | |
} | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment