js method to judge the variable as an array

  Before explaining how to judge an object as an array type, let's consolidate the data types of js. There are six data types in js: number, string, object, Boolean, null, and undefined.

  • string: Described by single or double quotes, such as "string";
  • number: array type, such as integer, decimal, etc.;
  • Boolean: has two values: true, false;
  • undefined: undefined, that is, you created a variable but did not assign a value to it;
  • null: As the name suggests, null means nothing, and means nothing;
  • object: Types other than the five above.
      js arrays are untyped: array elements can be of any type, and different elements in the same array may have different types. The elements of an array can be objects or other arrays, which allows the creation of complex data structures. Usually we can use the unary operator typeof to determine the data type of js, but for a special object such as an array, it can only return "object".
    typeof can solve most data type judgments. It is a unary operation. It is placed before an operation value, and its return value is a string. if(typeof(your value) == "string"){}.
    Here are some examples:
[javascript]  view plain copy  
  1. var str="string";  
  2.     console.log(str); //string  
  3.   
  4.     var  num=1;  
  5.     console.log(num); //number  
  6.   
  7.     var  bn= false ;  
  8.     console.log(bn); //boolean  
  9.   
  10.     var  a;  
  11.     console.log(typeof a); //undfined  
  12.   
  13.     var  obj =  null ;  
  14.     console.log(typeof obj); //object  
  15.   
  16.     var doc = document;  
  17.     console.log(typeof doc);//object  
  18.   
  19.     var  arr = [];  
  20.     console.log(arr); //object  
  21.   
  22.     var  fn =  function () {};  
  23.     console.log( typeof  fn);  //function can judge the function type in addition to the data type  

      除了前四个类型外,null、对象、数组返回的都是object类型;对于函数类型返回的则是function,再比如typeof(Date),typeof(eval)等。接下来进入正题,js判断数组类型的方法。

方法一: instanceof

      instanceof 用于判断一个变量是否某个对象的实例,左边操作数是一个对象,右边操作数是一个函数对象或者函数构造器。原理是通过判断左操作数的对象的原型链上是否具有右操作数的构造函数的prototype属性。
a instanceof b?alert("true"):alert("false")  //注意b值是你想要判断的那种数据类型,不是一个字符串,比如Array。
举一个例子:
[javascript]  view plain  copy
  1. var arr=[];  
  2. console.log(arr instanceof Array) //返回true   

方法二之 constructor

     在W3C定义中的定义:constructor 属性返回对创建此对象的数组函数的引用,就是返回对象相对应的构造函数。从定义上来说跟instanceof不太一致,但效果都是一样的。

那么判断各种类型的方法:

[javascript]  view plain  copy
  1. console.log([].constructor == Array);  
  2. console.log({}.constructor == Object);  
  3. console.log("string".constructor == String);  
  4. console.log((123).constructor == Number);  
  5. console.log(true.constructor == Boolean);  
注意:
      使用instaceof和construcor,被判断的array必须是在当前页面声明的!比如,一个页面(父页面)有一个框架,框架中引用了一个页面(子页面),在子页面中声明了一个array,并将其赋值给父页面的一个变量,这时判断该变量,Array ==object.constructor;会返回false;
原因:
1、array属于引用型数据,在传递过程中,仅仅是引用地址的传递。
2、每个页面的Array原生对象所引用的地址是不一样的,在子页面声明的array,所对应的构造函数,是子页面的Array对象;父页面来进行判断,使用的Array并不等于子页面的Array。

方法三 最简单的方法   Object.prototype.toString.call(arr) === “[object Array]”

[javascript]  view plain  copy
  1. function isArray(o) {  
  2.     return Object.prototype.toString.call(o) === ‘[object Array]‘;  
  3. }  
4.Array.isArray()  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324407266&siteId=291194637