Home  >  Article  >  Web Front-end  >  Detailed explanation of how to judge js data type and array method instance

Detailed explanation of how to judge js data type and array method instance

伊谢尔伦
伊谢尔伦Original
2017-07-18 11:37:221594browse

Data type judgment typeof

typeof can solve the judgment of most data types. Its return value is a string that describes the type of the operand.


//判断变量num是不是一个数字类型
if(typeof num=='number') {
  return true;
}

Return results:


var a="hling"; console.log(a); //string
var a=1; console.log(a); //number
var a=false; console.log(a); //boolean
var a; console.log(typeof a); //undfined
var a = null; console.log(typeof a); //object
var a = document; console.log(typeof a); //object
var a = []; console.log(a); //object
var a = function(){}; console.log(typeof a) 
//function除了可以判断数据类型还可以判断function类型

Except for the four types string, number, boolean, and undefined In addition, null, object, and array return all object types! ! !

For function types, function is returned, such as typeof(Date), typeof(eval), etc.

js method to determine array type

1) instanceof

instanceof is used to determine an Whether the variable is an instance of an object is a ternary operator. This operator has something to do with object-oriented in JavaScript. To understand this, you must first understand object-oriented in JavaScript. Because this operator detects whether the prototype chain of the object points to the prototype object of the constructor.


a instanceof b?alert("true"):alert("false") 
//注意b值是你想要判断的那种数据类型,是不是一个字符串,比如Array

Example:


##

var arr = [1,2,3,1]; 
alert(arr instanceof Array); // true

2) constructor

Definition in W3C definition: The constructor attribute returns a reference to the array function that created this object


var arr = []; 
arr instanceof Array; // true 
arr.constructor == Array; //true

The method for judging various types is:


console.log("string".constructor == String);
console.log((123).constructor == Number);
console.log(false.constructor == Boolean);
console.log([].constructor == Array);
console.log({}.constructor == Object);

General method:


function isArray(object){
  return object && typeof object==='object' &&
      Array == object.constructor;
}

3) Characteristic judgment

object.isArray() to determine, the purpose is to accurately detect whether a value is an array. IE9+, Firefox 4+, Safari 5+, Opera 10.5+ and Chrome all implement this method. However, versions before IE8 are not supported.


function isArray(object){
  return object && typeof object==='object' &&  
      typeof object.length==='number' && 
      typeof object.splice==='function' &&  
       //判断length属性是否是可枚举的 对于数组 将得到false 
      !(object.propertyIsEnumerable('length'));
}

4) Object.prototype.toString.call


Object.prototype.toString.call(value) == '[object Array]'

The above is the detailed content of Detailed explanation of how to judge js data type and array method instance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn