標籤:window var isp code return 基本 mis 字串 als
ECMAScript 標準定義了 7 種資料類型:Boolean、Null、Undefined、Number、String、Symbol(ES6新增)和Object,除Object以外的那6種資料類型也被稱為基礎資料型別 (Elementary Data Type),另外還有Array、Function等複雜資料類型。本文介紹一般類型判斷方法,最後總給一套全面的資料類型判斷方法。
一、typeof
typeof是一個一元運算子(不是一個函數方法),可以鑒別null以外的基礎資料型別 (Elementary Data Type)以及Object和Function。它的傳回值是小寫字串:
/**** typeof ****/typeof 37 ; //輸出 "number"typeof "aaa" ; //輸出 "string"typeof undefined ; //輸出 "undefined"typeof false; //輸出 "boolean"typeof {a:1,b:2}; //輸出 "object"typeof function(){}; //輸出 "function"//它不能鑒別null和arraytypeof null; //輸出 "object"typeof [1,2]; //輸出 "object"
二、Object.prototype.toString.call()
完美鑒別基礎資料型別 (Elementary Data Type)及Object、Function、Array、Date、Math等等類型
/**** Object.prototype.toString.call ****/Object.prototype.toString.call("aa"); //輸出 "[object String]"Object.prototype.toString.call(123); //輸出 "[object Number]"Object.prototype.toString.call(null); //輸出 "[object Null]"Object.prototype.toString.call(undefined); //輸出 "[object Undefined]"Object.prototype.toString.call([1,2,3]); //輸出 "[object Array]"Object.prototype.toString.call(function(){}); //輸出 "[object Function]"Object.prototype.toString.call({a:1,b:2}); //輸出 "[object Object]"
三、其他判斷方法
Array.isArray()可以判斷一個資料是否是數群組類型。
instanceof操作符用來測試一個對象在其原型鏈中是否存在一個建構函式的 prototype
屬性。某些情況下也能用來檢測資料類型,慎用。
/**** isArray判斷數組 ****/var arrTmp = [1,2];Array.isArray(arrTmp); //輸出true/**** instanceof判斷類型,不準確 ****/var numTmp1 = 123;var numTmp2 = new Number(123);numTmp1 instanceof Number; //輸出 falsenumTmp2 instanceof Number; //輸出 truearrTmp instanceof Array; //輸出 truearrTmp instanceof Object; //輸出 true
四、全套判斷方法
(下面這段代碼挪自Anguar源碼,稍加修整)
var toString = Object.prototype.toString;function isUndefined(value) { return typeof value === ‘undefined‘;}function isDefined(value) { return typeof value !== ‘undefined‘;}function isObject(value) { return value !== null && typeof value === ‘object‘;}function isString(value) { return typeof value === ‘string‘;}function isNumber(value) { return typeof value === ‘number‘;}function isDate(value) { return toString.call(value) === ‘[object Date]‘;}var isArray = Array.isArray;function isFunction(value) { return typeof value === ‘function‘;}function isRegExp(value) { return toString.call(value) === ‘[object RegExp]‘;}function isWindow(obj) { return obj && obj.window === obj;}function isFile(obj) { return toString.call(obj) === ‘[object File]‘;}function isFormData(obj) { return toString.call(obj) === ‘[object FormData]‘;}function isBoolean(value) { return typeof value === ‘boolean‘;}function isPromiseLike(obj) { return obj && isFunction(obj.then);}function isElement(node) { return !!(node && (node.nodeName || (node.prop && node.attr && node.find)));}function isArrayLike(obj) { if (obj == null || isWindow(obj)) { return false; } var length = "length" in Object(obj) && obj.length; if (obj.nodeType === 1 && length) { return true; } return isString(obj) || isArray(obj) || length === 0 || typeof length === ‘number‘ && length > 0 && (length - 1) in obj;}
鑒別JS資料類型的全套方法