码迷,mamicode.com
首页 > 其他好文 > 详细

Why does typeof array with objects return “Object” and not “Array”?

时间:2019-01-12 15:28:05      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:==   turn   lan   with   pre   isarray   class   cal   and   

https://stackoverflow.com/questions/4775722/check-if-object-is-an-array

One of the weird behaviour and spec in Javascript is the typeof Array is Object.

You can check if the variable is an array in couple of ways:

var isArr = data instanceof Array;
var isArr = Array.isArray(data);

But the most reliable way is:

isArr = Object.prototype.toString.call(data) == ‘[object Array]‘;

Since you tagged your question with jQuery, you can use jQuery isArray function:

var isArr = $.isArray(data);

 

Why does typeof array with objects return “Object” and not “Array”?

标签:==   turn   lan   with   pre   isarray   class   cal   and   

原文地址:https://www.cnblogs.com/chucklu/p/10259628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!