On the data type detection _javascript techniques of javascript

Source: Internet
Author: User
one, JavaScript data
JavaScript data is divided into two types: simple data and complex data. Simple data contains five of number,string,boolean,undefined and null; complex data is only one object. "Here friendship acknowledgement Li Shi teacher,<< Wu Javascript>> write too vivid, the impression is too profound"

second, JavaScript data type detection
1, the Omnipotent typeof
Let's start with a test to get a simple data type by typeof. Don't say anything, the code is kingly:
Copy Code code as follows:

Get the data type of the variable obj
function GetType (obj) {
Return typeof (obj);
}
/* Constant FETCH type * *
Alert (GetType (1)); Number
Alert (GetType ("Jeff Wong")); String
Alert (GetType (true)); Boolean
Alert (GetType (undefined)); Undefined
Alert (GetType (null)); Object
/* Variable FETCH 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, with the TypeOf operator, the previous four simple data types are entirely expected, but typeof null returns object. It should be noted that NULL is a unique value for a null type, but NULL is not object, and a variable with a null value is not object, so it is not possible to get the null type correctly by typeof. To get the simple data type right, just add a little improvement to the GetType area:
Copy Code code as follows:

function GetType (obj) {
return (obj = = null)? "Null": typeof (obj);
}

Then try the complex data type object:
Copy Code code as follows:

function Cat () {
}
Cat.prototype.CatchMouse = function () {
Do some thing
}
Get 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 see that, in addition to the function (note case), the function is returned, whether it is a common built-in object of JavaScript object,string or date, or a custom function that is returned by TypeOf without exception, It's all object. But for custom function, we prefer to get its "flesh" (in the example of cat rather than object), and obviously TypeOf does not have this conversion processing power.
2, constructor, want to say I love you loudly
Since there is no solution to the Universal typeof, how do we judge whether a variable is a custom function instance? We know that all the objects of JavaScript have a constructor attribute that can help us determine the object data type, especially for custom function:
Copy Code code as follows:

var obj = "Jeff Wong";
Alert (Obj.constructor = = String); True
obj = new Cat ();
Alert (Obj.constructor = = Cat); True

However, you can also test the following code:
Copy Code code as follows:

alert (1.constructor); Numeric constants error numeric constants no 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 constructor property
none = undefined;
alert (obj.constructor); Undefined no constructor properties

Experiments have shown that numeric constants, NULL, and undefined have no constructor properties.
Here, you will be as happy as the building pig to think that it is finally done? The following code may also have some heuristic and mining effects:
Copy Code code 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 understanding

Originally for the prototype chain inheritance, Constuctor is not so well. Then what?
3, the intuitive instanceof
Hey, please instanceof Grand debut. Look at its name, it seems to be to get an instance of an object, do not know how to understand this, right? Anyway, let's go ahead and improve the code test first:
Copy Code code 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 there is no doubt
Alert (Garfield instanceof Animal); True to understand

OK, about JavaScript data type detection, building pig is generally summed up here. I hope that the high man will add.
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.