判断对象或数组

1、Array.isArray()    判断数组时--首推

var a = [];
Array.isArray(a);  // true

var a ={};
Array.isArray(a);  // false

2、constructor属性   判断对象时--首推   (可判断 Arrray、Object、Number、Boolean、String)

var a = [];
a.constructor === Array     // true

var a = {};
a.constructor === Array    // false

var a = {};
a.constructor === Object   // true

3、instanceof    (可判断 Arrray、Object。不推荐)

var a =[];
a instanceof Array    // true

var a ={};
a instanceof Object   // true

4、$.isPlainObject() 
判断指定参数是否是一个纯粹的对象(所谓”纯粹的对象”,就是该对象是通过”{}”或”new Object”创建的。)

var a = {};  //  或 var a = new Object();
$.isPlainObject(a)   // true

5、typeof

// 根据typeof判断对象也不太准确
      表达式      返回值
typeof   undefined  'undefined'
typeof   null      'object'
typeof   true      'boolean'
typeof   123      'number'
typeof   "abc"     'string'
typeof   function(){} 'function'
typeof   {}       'object'
typeof   []       'object'

 


参考:https://www.cnblogs.com/ma-shuai/p/7805264.html

posted @ 2018-11-29 17:26  新码将  阅读(246)  评论(0编辑  收藏  举报