判斷數據類型的方法


typeof

如果使用typeof來判斷數據類型的話,結果如下:

console.log(
    typeof 123, //"number"
    typeof 'dsfsf', //"string"
    typeof false, //"boolean"
    typeof [1,2,3], //"object"
    typeof {a:1,b:2,c:3}, //"object"
    typeof function(){console.log('aaa');}, //"function"
    typeof undefined, //"undefined"
    typeof null, //"object"
    typeof new Date(), //"object"
    typeof /^[a-zA-Z]{5,20}$/, //"object"
    typeof new Error() //"object"
);

以上結果都是在chrome瀏覽器里運行結果,可以發現如下規律

Array,Object,null,Date,RegExp,Error這幾個類型都被typeof判斷為object,所以如果想要判斷這幾種類型,就不能使用typeof了。

Number,String,Boolean,Function,undefined,如果想判斷這幾種類型,那就可以使用typeof。

instanceof

除了使用typeof來判斷,還可以使用instanceof。instanceof運算符需要指定一個構造函數,或者說指定一個特定的類型,它用來判斷這個構造函數的原型是否在給定對象的原型鏈上。

結果如下:

console.log(
    123 instanceof Number, //false
    'dsfsf' instanceof String, //false
    false instanceof Boolean, //false
    [1,2,3] instanceof Array, //true
    {a:1,b:2,c:3} instanceof Object, //true
    function(){console.log('aaa');} instanceof Function, //true
    undefined instanceof Object, //false
    null instanceof Object, //false
    new Date() instanceof Date, //true
    /^[a-zA-Z]{5,20}$/ instanceof RegExp, //true
    new Error() instanceof Error //true
)

可以發現如下規律:

Number,String,Boolean沒有檢測出他們的類型,但是如果使用下面的寫法則可以檢測出來:

var num = new Number(123);
var str = new String('dsfsf');
var boolean = new Boolean(false);

還需要注意null和undefined都返回了false,這是因為它們的類型就是自己本身,並不是Object創建出來它們,所以返回了false。

constructor

constructor是prototype對象上的屬性,指向構造函數。根據實例對象尋找屬性的順序,若實例對象上沒有實例屬性或方法時,就去原型鏈上尋找,因此,實例對象也是能使用constructor屬性的。

如果輸出一個類型的實例的constructor,就如下所示:

console.log(new Number(123).constructor)
//ƒ Number() { [native code] }

可以看到它指向了Number的構造函數,因此,可以使用num.constructor==Number來判斷一個變量是不是Number類型的。

var num  = 123;
var str  = 'abcdef';
var bool = true;
var arr  = [1, 2, 3, 4];
var json = {name:'wenzi', age:25};
var func = function(){ console.log('this is function'); }
var und  = undefined;
var nul  = null;
var date = new Date();
var reg  = /^[a-zA-Z]{5,20}$/;
var error= new Error();

function Person(){
  
}
var tom = new Person();

// undefined和null沒有constructor屬性
console.log(
    tom.constructor==Person,
    num.constructor==Number,
    str.constructor==String,
    bool.constructor==Boolean,
    arr.constructor==Array,
    json.constructor==Object,
    func.constructor==Function,
    date.constructor==Date,
    reg.constructor==RegExp,
    error.constructor==Error
);
//所有結果均為true

除了undefined和null之外,其他類型都可以通過constructor屬性來判斷類型。

使用toString()檢測對象類型

可以通過toString() 來獲取每個對象的類型。為了每個對象都能通過 Object.prototype.toString() 來檢測,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式來調用,傳遞要檢查的對象作為第一個參數,稱為thisArg。

var toString = Object.prototype.toString;

toString.call(123); //"[object Number]"
toString.call('abcdef'); //"[object String]"
toString.call(true); //"[object Boolean]"
toString.call([1, 2, 3, 4]); //"[object Array]"
toString.call({name:'wenzi', age:25}); //"[object Object]"
toString.call(function(){ console.log('this is function'); }); //"[object Function]"
toString.call(undefined); //"[object Undefined]"
toString.call(null); //"[object Null]"
toString.call(new Date()); //"[object Date]"
toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
toString.call(new Error()); //"[object Error]"

這樣可以看到使用Object.prototype.toString.call()的方式來判斷一個變量的類型是最准確的方法。

封裝一個獲取變量准確類型的函數

function gettype(obj) {
  var type = typeof obj;

  if (type !== 'object') {
    return type;
  }
  //如果不是object類型的數據,直接用typeof就能判斷出來

  //如果是object類型數據,准確判斷類型必須使用Object.prototype.toString.call(obj)的方式才能判斷
  return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1');
}

這樣判斷一個變量的數據類型就很方便了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM