Introduction to javascript data type detection _ javascript skills

Source: Internet
Author: User
Analyze the javascript data types. This time, we will only briefly discuss javascript data type detection. We hope you will continue to give your comments. I. javascript data
Javascript data is divided into two types: simple data and complex data. Simple data includes five types: number, string, boolean, undefined, and null. There is only one type of complex data: object. [Here, I would like to thank Miss Li Zhan, < <悟透javascript> > I was so impressed with my writing]

Ii. javascript data type detection
1. omnipotent typeof
Let's first test how to obtain simple data types through typeof. Let alone anything. The code is king:

The Code is as follows:


// Obtain the data type of the variable obj
Function getType (obj ){
Return typeof (obj );
}
/* Constant acquisition type */
Alert (getType (1); // number
Alert (getType ("jeff wong"); // string
Alert (getType (true); // boolean
Alert (getType (undefined); // undefined
Alert (getType (null); // object
/* Variable acquisition type */
Var num = 1;
Var str = "jeff wong ";
Var flag = true;
Var hell = undefined;
Var none = null;
Alert (getType (num); // number
Alert (getType (str); // string
Alert (getType (flag); // boolean
Alert (getType (hell); // undefined
Alert (getType (none); // object


As you can see, through the typeof operator, the first four simple data types are completely expected, but typeof null returns the object. It should be noted that null is the unique value of the null type, but null is not an object and a variable with a null value is not an object. Therefore, the null type cannot be obtained directly through typeof. To correctly obtain simple data types, you only need to add some improvements to getType:

The Code is as follows:


Function getType (obj ){
Return (obj = null )? "Null": typeof (obj );
}


Next, try the complex data type object:

The Code is as follows:


Function Cat (){
}
Cat. prototype. CatchMouse = function (){
// Do some thing
}
// Obtain the data type of the variable obj
Function getType (obj ){
Return (obj = null )? "Null": typeof (obj );
}
Var obj = new Object ();
Alert (getType (obj); // object
Var func = new Function ();
Alert (getType (func); // function
Var str = new String ("jeff wong ");
Alert (getType (str); // object
Var num = new Number (10 );
Alert (getType (num); // object
Var time = new Date ();
Alert (getType (time); // object
Var arr = new Array ();
Alert (getType (arr); // object
Var reg = new RegExp ();
Alert (getType (reg); // object
Var garfield = new Cat ();
Alert (getType (garfield); // object


We can see that except Function (case sensitive), function is returned, whether it is a common built-in javascript Object, String or Date, or a custom function, no exception is returned through typeof, all are objects. However, for a custom function, we prefer to get its "True Colors" (In the example, Cat, rather than object). Obviously, typeof does not have this conversion processing capability.
2. constructor
Since the omnipotent typeof has no solution, how can we determine whether a variable is a custom function instance? We know that all objects in javascript have a constructor attribute, which can help us determine the object data type, especially for custom functions:

The Code is as follows:


Var obj = "jeff wong ";
Alert (obj. constructor = String); // true
Obj = new Cat ();
Alert (obj. constructor = Cat); // true


However, you can test the following code:

The Code is as follows:


// Alert (1. constructor); // numeric constant error. Numeric constant does not have constructor
Var num = 1;
Alert (num. constructor = Number); // true
Alert ("jeff wong". constructor = String); // true
Var str = "jeff wong ";
Alert (str. constructor = String); // true
Var obj = null;
Alert (obj. constructor); // null does not have the constructor attribute
None = undefined;
Alert (obj. constructor); // undefined does not have the constructor attribute


Experiments show that numeric constants, null, and undefined do not have the constructor attribute.
So far, will you feel as lucky as Lou pig that you have finally achieved success? The following code may be somewhat enlightening and useful for mining:

The Code is as follows:


Function Animal (){
}
Function Cat (){
}
Cat. prototype = new Animal ();
Cat. prototype. CatchMouse = function (){
// Do some thing
}
Var obj = new Cat ();
Alert (obj. constructor = Cat); // false ??
Alert (obj. constructor = Animal); // true


The original prototype chain inheritance, constuctor is not so good. What should we do?
3. Intuitive instanceof
Hey, instanceof is coming soon. According to its name, it seems to be an instance for obtaining an object. I don't know if this is the case, right? In any case, let's test the above Code first:

The Code is as follows:


Function Animal (){
}
Function Cat (){
}
Cat. prototype = new Animal ();
Cat. prototype. CatchMouse = function (){
// Do some thing
}
Var garfield = new Cat ();
Alert (garfield instanceof Cat); // true no doubt
Alert (garfield instanceof Animal); // true


Okay. For javascript data type detection, Lou pig will give a general summary here. Hope to be supplemented by high-minded people.
Related Article

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.