JS判斷各種數據類型的方法&判斷一個變量是否爲數組類型

一、js中判斷數據類型的方法

js的七種數據類型:number、string、boolean、undefined、object、null、symbol和函數類型function。

方法一:typeof ,無法檢驗出數組、對象或null

typeof   2                  //  number
typeof   null               //   object
typeof   {}                 //   object
typeof   []                 //   object
typeof   (function(){})     //  function
typeof   undefined          //  undefined
typeof   '222'              //   string
typeof   true               // boolean

方法二:instanceof ,只能用來判斷數組和對象,不能判斷string和boolean類型,數組也屬於對象。

 var o = {'name':'lee'};
 var a = ['reg','blue'];

 console.log(o instanceof Object);// true
 console.log(a instanceof Array);//  true
 console.log(o instanceof Array);//  false

由於數組也屬於對象,不能區分是數組還是對象,改進:封裝一個方法判斷數組和對象

var o = {'name':'lee'};
var a = ['reg','blue'];

var getDataType = function(o){
            if(o instanceof Array){
                return 'Array'
            }else if( o instanceof Object ){
                return 'Object';
            }else{
                return 'param is no object type';
            }
       };

console.log(getDataType(o));//Object。
console.log(getDataType(a));//Array。

方法三:constructor方法

var o = {'name':'lee'};
var a = ['reg','blue'];
console.log(o.constructor == Object);//true
console.log(a.constructor == Array);//true

方法四:toString()判斷string和boolean類型

var o = {'name':'lee'};
var a = ['reg','blue'];

function c(name,age){
         this.name = name;
         this.age = age;
 }
var c = new c('kingw','27');
console.log(Object.prototype.toString.call(a));//[object Array]
console.log(Object.prototype.toString.call(o));//[Object Object]
console.log(Object.prototype.toString.call(c));//[Object Object]

二、特殊值的相等於嚴格相等比較


(function(){
    console.log(null==undefined) // 輸出:true
    console.log(null===undefined) // 輸出:false
    console.log(null===null) // 輸出:true
    console.log(undefined===undefined) // 輸出:true
    console.log(NaN==undefined) // 輸出:false
    console.log(NaN==null)  // 輸出:false
    console.log(NaN==NaN)  // 輸出:false
    console.log(NaN===NaN)  // 輸出:false
})()

三、字符串相等(==),嚴格相等(===)運算符

在進行相等(==)運算比較時,如果一邊是字符,一邊是數字,會先將字符串轉換成數字再進行比較

嚴格相等(===)則不會進行類型轉換,會比較類型是否相等;注NaN與任何值比較時都是false。

四、字符串大小的比較,大於(>), 小於(<)運算符

javascript字符串在進行大於(小於)比較時,會根據第一個不同的字符的ascii值碼進行比較,當數字(number)與字符串(string)進行比較大小時,會強制的將數字(number)轉換成字符串(string)然後再進行比較

五、JS判斷一個變量是否是數組類型

參考博客:https://blog.csdn.net/lee_magnum/article/details/11555981

1、instanceof操作符

instanceof 運算符用來檢測 constructor.prototype 是否存在於參數 object 的原型鏈上。

obj instanceof Object;     //true 實例obj在不在Object構造函數中

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

2、對象的constructor屬性

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

 第2種和第3種方法貌似無懈可擊,但是實際上還是有些漏洞的,當你在多個frame中來回穿梭的時候,這兩種方法就亞歷山大了。由於每個iframe都有一套自己的執行環境,跨frame實例化的對象彼此是不共享原型鏈的,因此導致上述檢測代碼失效! 

3、Object.prototype.toString  (用來區分null和array類型)

function isArrayFn (o) {  
 return Object.prototype.toString.call(o) === '[object Array]';   
}
var arr = [1,2,3,1];   
 
alert(isArrayFn(arr));// true 

 call改變toString的this引用爲待檢測的對象,返回此對象的字符串表示,然後對比此字符串是否是'[object Array]',以判斷其是否是Array的實例。

4.Array.isArray()   

ECMAScript5將Array.isArray()正式引入JavaScript,目的就是準確地檢測一個值是否爲數組。IE9+、 Firefox 4+、Safari 5+、Opera 10.5+和Chrome都實現了這個方法。但是在IE8之前的版本是不支持的。 

var arr = [1,2,3,1];  
var arr2 = [{ abac : 1, abc : 2 }];  
 
function isArrayFn(value){
	if (typeof Array.isArray === "function") {
		return Array.isArray(value);    
	}else{
		return Object.prototype.toString.call(value) === "[object Array]";    
	}
}
alert(isArrayFn(arr));// true 
alert(isArrayFn(arr2));// true 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章