判断变量是数组还是对象

① 通过typeof 加长度判断;

② 通过instanceof判断,不过有判断顺序;

 

以下为示例:

var judgeType1 = function(o){
    if(typeof o == 'object'){
       if(typeof o.length == 'number'){
           alert('变量类型为 Array')
           return
       }
        alert('变量类型为 Object')
        return
    }
    alert('变量类型不为 Object')
}
var judeType2 = function(o){
   if(o instanceOf Array){
        alert('变量类型为 Array')
   }else if(o instanceOf Object){
       alert('变量类型为 Object')
   }else{
       alert('变量类型不为 Object')
   }
}

 

posted @ 2019-08-22 14:52  ClaudiaYan  阅读(192)  评论(0编辑  收藏  举报