判斷資料型別

2022-07-18 05:54:08 字數 371 閱讀 9093

//

判斷乙個資料型別的函式封裝的方法,obj是用來接收傳入的引數的

function

gettype(obj)

//如果不是object型別的資料,直接用typeof就能判斷出來

//如果是object型別資料,準確判斷型別必須使用object.prototype.tostring.call(obj)的方式才能判斷

return object.prototype.tostring.call(obj).replace(/^\[object (\s+)\]$/, '$1');

//.replace(/^\[object (\s+)\]$/, '$1'); 主要是把 第乙個物件去掉

}

判斷資料型別

typeof 如果使用typeof來判斷資料型別的話,結果如下 var num new number 123 var str new string 1223 var bool new boolean false console.log typeof 123,number typeof num,obj...

判斷資料型別

typeof 判斷基本資料型別 不能區分null object 弊端不能區分 陣列 物件 和 null console.log typeof dddd console.log typeof 12 console.log typeof true console.log typeof undefined...

資料型別判斷

可以判斷基本資料型別,它返回的資料型別的字串 返回結果只能包括number,boolean,string,function,object,undefined 但不能判斷null array,可以使用typeof判斷變數是否存在 如if typeof a undefined 但是對於一些建立的物件,它...