识别数组对象

Mai*_*pal 22 javascript jquery

如何知道对象是否是数组?

 var x=[];

console.log(typeof x);//output:"object"
alert(x);//output:[object Object]
console.log(x.valueOf())//output:<blank>? what is the reason here?
console.log([].toString()); also outputs <blank>     
Object.prototype.toString.call(x) output:[object Array] how?
Run Code Online (Sandbox Code Playgroud)

自console.log([].toString()); 输出:空白

第一名:

为什么我在最后的第二个陈述中空白?

第二:

有没有办法确切知道对象是什么:数组或普通对象({})没有x.join()等各自方法的帮助,表明x是一个数组,而不是这种方式.

实际上,在jquery选择中,如$("p")返回jquery对象,所以如果我使用

console.log(typeof $("p"));//output:"object
Run Code Online (Sandbox Code Playgroud)

我只是想知道对象的实际名称.请它.谢谢你的帮助

Vis*_*ioN 11

在纯JavaScript中,您可以使用以下跨浏览器方法:

if (Object.prototype.toString.call(x) === "[object Array]") {
    // is plain array
}
Run Code Online (Sandbox Code Playgroud)

jQuery有特殊的方法:

if ($.isArray(x)) {
    // is plain array
}
Run Code Online (Sandbox Code Playgroud)