1. 程式人生 > >js判斷資料型別

js判斷資料型別

方法一:通過原型方法判斷

  1. 判斷value的資料型別:
let isArr=Object.prototype.toString.call(value) == "[ object.Array]";
let isObject=Object.prototype.toString.call(value) == "[ object.Object]";
let isFunction=Object.prototype.toString.call(value) == "[ object. Function]";
let isRegExp=Object.prototype.toString.call(value) == "[ object.RegExp]";
if(isArr){
  console. log("資料型別為陣列")
}
if(isObject){
   console. log("資料型別是物件。")
}
if(isFunction){
  console. log("資料型別是函式")
}
if(isRegExp){
   console. log("是正則表示式")
}