JavaScript variables to determine the type of

Javascript total of six data types: number, string, object, Boolean, null, undefined.

var str="string";
console.log(typeof str); //string
var num=1;
console.log(typeof num); //number
var bn=false;
console.log(typeof bn); //boolean
var a;
console.log(typeof a); //undfined
var obj = null;
console.log(typeof obj); //object
var doc = document;
console.log(typeof doc);//object
var arr = [];
console.log(arr); //object
var fn = function(){};
console.log(typeof fn); //function   

The normal type front four own type display, behind the null, the object array is returned object type; function returns the type of function.

JavaScript array type determination method:

Method 0: Use the isArray () 
var ARR = []; 
Alert (Array.isArray (obj)); 

Method a: Method using instanceof 
instanceof determines whether a variable instance for an object, the left operand is an object, the right operand is a function object constructor or function. The principle is the determination target left operand whether the prototype chain constructor prototype property right operand. 
a instanceof b alert ( "true" ):? alert ( "false") // Note b the value of the data type is the kind you want to judge, not a string, such as Array. 
As an example: 
var ARR = []; 
the console.log (ARR the instanceof the Array) // returns true 

two: constructor method 
defined in the W3C definition: constructor function to create an array of property returns a reference to the object is returned object should opposing constructor. By definition it is not consistent with instanceof, but the effect is the same. 
Then determining various types of methods: 
the console.log (. [] == the Array constructor); // to true 
the console.log (. {} == Object constructor); // to true 
the console.log ( "String" .constructor = String =); // to true 
the console.log ((123) == Number The .constructor); // to true 
console.log (true.constructor == Boolean); // to true
Note: 
Use instaceof and construcor, the array must be judged on the current page statement! For example, a page (parent page) has a frame, which is referenced in a page (subpages), declares an array in the sub-page, and assign it to a variable parent page, then the variable is determined, Array == object.constructor; return false; 
reasons: 
. 1, Array data belonging to a reference type, in the transfer process, only the address transmitted by reference. 
2, the address of each page Array native objects referenced are not the same, the sub-page array declaration, the corresponding constructor, Array object is a sub-page; parent page to be judged, the use does not mean Array Array subpages. 

Method 3: Use Object.prototype.toString.call (arr) === '[object Array]' method 
function the isArray (O) { 
  return Object.prototype.toString.call (O); 
} 
var ARR = [2,5 , 6,8]; 
var obj = {name: 'zhangsan', Age: 25}; 
var Fn = function () {} 
the console.log (the isArray (ARR)); // [Object the Array]  
the console.log (the isArray (obj)); // [object Object]
the console.log (the isArray (fn)); // [object function]

  

Reference: https: //www.cnblogs.com/lingdu87/p/9152806.html

Reference: https: //www.cnblogs.com/linda586586/p/4227146.html

Reference: https: //blog.csdn.net/u010297791/article/details/55049619

Guess you like

Origin www.cnblogs.com/miaolyou/p/10942386.html