目的:加強js的資料型別檢測
結果:返回null, undefined, string, number, boolean, object, function, array, date, regexp
原理:undefined,string,number,boolean利用typeof檢測,null直接返回null,其他的利用tostring.call()返回再取json中的對應值。
**:
/*自定義工具物件start*/
(function(window, undefined);
var tostring = class2type.tostring;
var typearr = ['boolean', 'number', 'string', 'function', 'array', 'date', 'regexp', 'object', 'error', 'symbol'];
typearr.foreach(function(value, index, array) );
var jquery =
return typeof ele === "object" || typeof ele === "function" ?
class2type[ tostring.call( ele ) ] || "object" :
typeof ele;}};
window.jquery = window.$ = jquery;
})(window);
/*自定義工具物件end*/
var elenull = null;
var eleundefined = undefined;
var elestring = "123";
var elebool = true;
var elenumber = 123;
var elearray = ;
var elefun = function(){};
var eleobj = {};
var eledate = new date();
var elereg = new regexp();
console.log($.type(elenull));
console.log($.type(eleundefined));
console.log($.type(elestring));
console.log($.type(elebool));
console.log($.type(elenumber));
console.log($.type(elearray));
console.log($.type(elefun));
console.log($.type(eleobj));
console.log($.type(eledate));
console.log($.type(elereg));
js檢測資料型別
要檢測乙個變數是不是基本資料型別?typeof 操作符是最佳的工具。說得更具體一 點,typeof 操作符是確定乙個變數是字串 數值 布林值,還是undefined 的最佳工具。如果變 量的值是乙個物件或null,則typeof 操作符會像下面例子中所示的那樣返回 object var s nich...
JS資料型別檢測
在js的日常使用中,經常需要檢測資料的型別,現在,就來看一下,js有哪些方法可以檢測資料的型別。typeof操作符返回乙個字串,表示未經計算的運算元的型別。typeof是js原生提供用來檢測型別的api,然而,並沒有什麼用。為什麼呢?因為,typeof不能準確地檢測出我們所想要知道的資料的型別。ty...
JS 資料型別檢測
tpeof val 用來檢測資料型別的運算子。基於typeof檢測出來的結果 首先是乙個字串 字串中是對應的型別 侷限性 typeof null object 但是null並不是物件 基於typeof 無法細分出當前值是普通物件還是陣列物件等,只要是物件型別,返回結果都是 object typeof...