The Js type detection method is a small piece of Js knowledge system.
1. There are five basic data types in Js: Undefined, Null, Boolean, String, Number (including NaN) NaN and any types of values, including NaN; isNaN is used to determine whether the value is NaN type 2. Type determination 1. isFinite (number) is not infinite. If it is not true, if it is NaN, or positive or negative infinity, or if it is not a number, false 2 is returned. when the typeof operator is used, the value stringnumberbooleanundefinedfunctionobject returned by space or typeof (param) also returns the object based on the above. The judgment type can be as follows: var obtainType = function (o) {var t; if (o = null) return "null"; else if (o! = O) return "NaN"; else if (t = typeof o )! = 'Object') return t;} can identify null, NaN string number boolean undefined function. Only objects are left above, such as array recognition and custom type recognition. the following function obtainType (type) {return function (obj) {return Object can be used to identify native types such as arrays. prototype. toString. call (obj) = "[object" + type + "]"} var isObject = isType ("Object") var isString = isType ("String ") var isArray = Array. isArray | isType ("Array") var isFunction = isType ("Function") 4. user-Defined type judgment/*** returns the Function name, which may be an empty string. If it is not a Function, null */Function is returned. prototype. getName = func Tion () {if ("name" in this) return this. name; return this. name = this. toString (). match (/function \ s * ([^ (] *) \ (/) [1] ;}; both native and custom types can be judged, so/*** return: null NaN undefined string number boolean * function Array String Object (including some custom types) custom type */var obtainType = function (o) {/*** get parameter type * Object direct volume, Object. the class attributes of create and custom constructor are all objects. * The native type (built-in constructor and Host Object) is recognized */function classOf (obj) {return Object. proto Type. toString. call (obj ). slice (8,-1);}/*** returns the Function name, which may be a null String. If it is not a Function, null */Function is returned. prototype. getName = function () {if ("name" in this) return this. name; return this. name = this. toString (). match (/function \ s * ([^ (] *) \ (/) [1] ;}; var t, c, n; // processing null values in special cases if (o = null) return "null"; // NaN: not equal to its own value if (o! = O) return "NaN"; // identifies native value types and functions, undefined if (t = typeof o )! = "Object") return t; // identifies the native type if (c = classOf (o ))! = "Object") return c; // return the User-Defined Type constructor name if (o. constructor & typeof o. constructor = "function" & (n = o. constructor. getName () return n; return "Object" ;}; 5. var strObj = new String ('abc'); typeof strObj // "object" obtainType (strObj) // "String" III. Other 1. determine if (Dom. nodeType ){... dom ...} if (dom. createElement) 2. jQuery and other types identification $ ('# A') instanceof jQuery // cross-window and frame subpages are not supported. 3. if (a) a is null undefined 0 "" NaN is automatically converted to false. The recommended method is // badif (name! = ''){//... Stuff ...} // goodif (name ){//... stuff ...} // badif (collection. length> 0 ){//... stuff ...} // goodif (collection. length ){//... stuff ...}