in front-end development, it is often necessary to use the requirements for detecting variable data types, such as: Judging whether a variable is undefined or null for the next step, today reading "Writing high-quality code-188 suggestions for improving JavaScript programs" This book is a good way to see this in a little bit to be used in future development.
1: Shown below is the demo code I wrote
<!DOCTYPE HTML><HTML> <Head> <MetaCharSet= "Utf-8"/> <title>Index Page</title> <Scripttype= "Text/javascript"> /*security detection JavaScript basic data type and built-in object @param: O represents the detected value @return: Returns the string ' undefined ', ' Number ', ' Boolean ', ' Stri Ng ', ' function ', ' regexp ', ' array ', ' Date ', ' Error ', ' object ', ' null '*/ functiontypeOf (o) {//gets a reference to the default ToString () method of object objects, because the only way to get the class value of an object is to call the default ToString () method defined by the objects object, and the different objects will predefine their own ToString () method. The logic of the transformation is different var_tostring=Object.prototype.toString; //enumerates the basic data types and built-in object types to further complement the range of detection data types for the array var_type= { 'undefined' : 'undefined', ' Number' : ' Number', 'Boolean' : 'Boolean', 'string' : 'string', '[Object Function]' : 'function', '[Object RegExp]' : 'RegExp', '[Object Array]' : 'Array', '[Object Date]' : 'Date', '[Object Error]' : 'Error' } //returns the corresponding data type or string representation of the built-in object return_type[typeofO]||_type[_tostring.call (o)]||(o? 'Object' : 'NULL'); } /*test Method*/ (functiontesttypeof () {varobj={'name':'godtrue'}, Num=1, str='Hello', Boo=true, ABS=math.abs, DAT=NewDate (), arr=NewArray (), Err=NewError (), Reg=NewRegExp (); Console.info (typeOf (obj)); Console.info (typeOf (num)); Console.info (TypeOf (str)); Console.info (TypeOf (boo)); Console.info (TYPEOF (ABS)); Console.info (typeOf (DAT)); Console.info (TypeOf (arr)); Console.info (TypeOf (err)); Console.info (TypeOf (reg)); Console.info (TypeOf (NULL)); Console.info ('"Author:godtrue"'); })(); </Script> </Head> <Bodybgcolor= "AliceBlue"Align= "Center"> <Pstyle= "color:red;">Note: The above method applies to JavaScript basic data types and built-in objects, but not to custom objects. This is because the return value is not regular after the custom object is converted to a string, and the return values of the different browsers are different. Therefore, to detect the built-in object, you can only use the constructor property and the instaceof operator. </P> </Body></HTML>
2: The following shows the performance of the demo code, as well as considerations
3: summary
The code needs to be noted in the note, the code as a whole is very simple three sentences, but this method is more useful, I believe in future projects will also be used. According to my previous experience, judging an object or variable "non-null or not", "definition undefined or not" is the most common, this method has certain universality, can also be expanded according to their own needs.
In addition, yesterday I suddenly found their blog, some sites privately reproduced and did not mention the article from the blog Park, the author of the information is also all erased, so I feel not very good, although, I write the purpose of the blog is to record their technical accumulation and feelings, mainly for their future use, Also very happy to let other need to see or reprint, but please respect the labor of others pay, at least mark the source of the article!
js-Secure method for detecting JavaScript base data types and built-in objects