Let's take a look at one of my interview questions.
Last week in the interview, the Examiner asked me how JS to determine the data type of a variable is an array, JS typeof can not distinguish between array and Object, this is a problem of JS. Other words:
typeof [] = = ' object '; True to determine the array is as follows: (too long for line, we will see)
| The code is as follows |
Copy Code |
if (a&&typeof a = = ' object ' &&typeof a.length = ' number ' && !a.propertyisenumerable (' length ')) { Alert ("A is an array"); } |
Well, I do not know that we understand, did not understand and then look.
One, JS in which data types.
1. Numeric type (number). Include integers, floating-point numbers.
2. Boolean type (Boolean).
3. String type.
4. Objects (object).
5. Arrays (array).
6. Null value (NULL).
7. Not defined (Undefined).
Second, determine what data type a variable belongs to.
1. Numeric type (number).
A more commonly used method of judgment is
| The code is as follows |
Copy Code |
function Isnumber (val) { Return typeof val = = ' number '; }
|
But there are some things you can't do. Like what
var A;
document.write (Isnumber (parseint (a)));
The print here is true, but in fact the variable A is Nan, and it cannot be used for numeric operations.
So the above function can be modified to
| The code is as follows |
Copy Code |
function Isnumber (val) { Return typeof val = = ' number ' && isfinite (val); }
|
The Isfinite () function is JS-led, which filters out nan and infinity.
In addition, to determine whether a variable is Nan, you can use isNaN (), which returns a Boolean value.
2. Boolean (Boolean), String, and undefined (Undefined).
These 3 types are relatively simple, direct use
| The code is as follows |
Copy Code |
typeof val = = ' Boolean ' typeof val = = ' String ' typeof val = = ' undefined '
|
It's OK.
3. Objects (object) and Null values (NULL).
Because the TypeOf returns object when the variable is null, the object cannot be used directly
typeof judgment. It should be.
| The code is as follows |
Copy Code |
function Isobj (str) { if (str = = NULL | | typeof str = = ' undefined ') { return false; } return typeof str = = ' object '; }
|
The null value can be judged by val = =. Be careful with congruent.
4. Arrays (array).
Array types cannot be judged by typeof. Because when the variable is an array type, TypeOf returns object.
Here are two ways to determine the type of an array.
| The code is as follows |
Copy Code |
function IsArray (arr) { return Object.prototype.toString.apply (arr) = = ' [Object Array] '; } Or function IsArray (arr) { return arr.constructor = = Array; } |
Let's share some common functions for validating data types
| The code is as follows |
Copy Code |
| One. Validation class *****************************/ Whether an object exists function Isobj (str) { if (str==null| | typeof (str) = = ' undefined ') return false; return true; } Remove spaces from a string function Strtrim (str) { if (!isobj (str)) return ' undefined '; Str=str.replace (/^/s+|/s+$/g, ""); return str; } /**********************1 Digital Verification ******************************/ 1. 1 integers An integer or an empty function Isintornull (str) { if (!isobj (str))//To determine whether an object exists return ' undefined '; return IsNull (str) | | Isint (str); } Must be an integer function Isint (str) { var reg =/^ (-|/+)?/d+$/; return Reg.test (str); } 1.2 Decimal Decimal or empty function Isfloatornull (str) { if (!isobj (str))//To determine whether an object exists return ' undefined '; if (Isint (str)) return true; return IsNull (str) | | Isfloat (str); } Must be decimal function Isfloat (str) { if (Isint (str)) return true; var reg =/^ (-|/+)?/d+/./d*$/; return Reg.test (str); } 1.3 Number Size Judgement Number I cannot be greater than number Y function Iminy (i, y) { if (!isobj (i) | |! Isobj (y))//To determine whether an object exists return ' undefined '; if (!) ( Isfloat (i) &&isfloat (y))) Return ' must be a number type ' comparison if (i<=y) return true; return false; } Number I cannot be less than number Y function Imaxy (i, y) { if (!isobj (i) | |! Isobj (y))//To determine whether an object exists return ' undefined '; if (!) ( Isfloat (i) &&isfloat (y))) Return ' must be a number type ' comparison if (i>=y) return true; return false; } /**********************1 Digital Verification ******************************/ /**********************2 Time Class Validation ******************************/ 2.1 Short time, form (13:04:06) function Shorttimecheck (str) { if (!isobj (str))//To determine whether an object exists return ' undefined '; var a = Str.match (/^ (/d{1,2}) (:)? /d{1,2})/2 (/d{1,2}) $/); var a = Str.match (/^/d{1,2}:/d{1,2}:/d{1,2}$/); if (a = = null) { Alert ("Input parameter is not a time format"); return false; } if (a[1]>24 | | a[3]>60 | | a[4]>60) { Alert ("Time format is not"); return False } return true; } 2.2 Short Date, form (2003-12-05) function Shordatecheck (str) { var r = Str.match (/^ (/d{1,4}) (-|//) (/d{1,2})/2 (/d{1,2}) $/); if (r==null) return false; var d= new Date (r[1], r[3]-1, r[4]); Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]); } 2.3 Long time, shaped like (2003-12-05 13:04:06) function Longdatecheck (str) { var reg =/^ (/d{1,4}) (-|//) (/d{1,2})/2 (/d{1,2}) (/d{1,2}):(/d{1,2}):(/d{1,2}) $/; var r = Str.match (reg); if (r==null) return false; var d= new Date (r[1], r[3]-1,r[4],r[5],r[6],r[7]); Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]&&d.gethours () = =r[5]&&d.getminutes () ==r[6]&&d.getseconds () ==r[7]); } 2.4 is a month and a half. Form (2003-05, or 2003-5) 2.5 hours and minutes, shaped like (12:03) |