Shallow play JavaScript data type judgment

Source: Internet
Author: User

Objective

Usually when you need to make type judgments, pick up typeof like a pistol and just burst into the dodo ... Nor do we have a thorough understanding of its specific characteristics.
So here is the use of free time, to do a more detailed understanding.

first, Let's take a full look at the typeof type of detection:

Here I give a set of more detailed data type Variables:

1     vars =NewString (' ABC '),2n = 123,3b =true,4A =NewArray (),5o =NewObject (),6D =NewDate (),7U =undefined,8f =function(){},9W =window,TenDoc =document, oneR =NewREGEXP (), am =Math, -t =window.settimeout, -_s = ' 123 ', the_n =NULL, -_d =document.body, -_nan = nan;

Here we use TypeOf to detect the data types of these variables, and see the specific return results:

 typeof s-to  string  typeof n-- number  typeof B--and  boolean
    typeof a-- Object  typeof o--objecttypeof D-and objecttypeof u-and  undefine D  typeof F-and  function  typeof w-and objecttypeof doc--objecttypeof R--and Objecttyp EOF M--objecttypoef t-and functiontypeof _s--numbertypoef _n--objecttypeof _d--objecttypeof _na --and number 

Through the above tests, it is quite certain that typeof can only check up to 5 data types: string,number,boolean, Underfined,object .
In fact, JavaScript has only these 5 data types.
Such data as date, Array, Function, htmlelement, Math, regexp, in javascript, they are all re-wrapped in object objects, so using typeof to detect them returns the object Type.
But in practical use, it is impractical to treat Date, Array, Function, htmlelement, Math, RegExp As Object Types.
fortunately, We can derive information from the constructors (constructor) of these objects to represent their specific meanings.

For example:

A.constructor.tostring ()--> "function Array () {[native code]}";

So by combining the two methods of typeof and object.constructor, we have written a method of detecting type:

1 functiongetType () {2 3     varValue = Arguments[0],4Type =function(){5             return/\w+\s (\w+) \ (\) \s.+?/.test (value.constructor.toString ())? Regexp.$1.tolowercase (): ' arguments except '; 6         };7 8     if(typeofValue) {9         returnvalue!==NULL? value!== undefined? Type (): ' undefined ': ' null ';Ten}Else{ one         return' Undefined '; a     } -  -}

There is also a little trick for typeof to Use. When using typeof to detect a data type that does not have a "variable", it does not error, but returns Undefined.
In the past, we tried to run the code with an indeterminate number by Try{}catch (e) {...}.

finally, when reviewing Typeof's Information. By the way, I also briefly looked at the introduction of the next instanceof Operator.
In general, the instanceof operator is used to "detect if the left argument is an instance of the right argument." If yes, returns true, otherwise falseis Returned.
Example:

1 var New person (); 2 instanceof // -- true;

In this example, because P is an instance of the person of the constructor, the instanceof operator is True.
This can be enlisted in p.constructor = = ' function person () {} ' .
A little more complicated, too:

1 functionperson () {}2 functionAnimal () {}3 4Persion.prototype =NewAnimal ();5 varp =Newperson ();6PinstanceofPersion ()//-- true7PinstanceofAnimal ()//-- true

----------------'s Good. Go to sleep ...---------continue to struggle tomorrow!!!! ---------------

Shallow play JavaScript data type judgment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.