Skip to content

Instantly share code, notes, and snippets.

@6174
Created August 11, 2013 09:48
Show Gist options
  • Save 6174/6204199 to your computer and use it in GitHub Desktop.
Save 6174/6204199 to your computer and use it in GitHub Desktop.
isArrayLike
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