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. [Friendly thanks to Teacher Li Zhan here, <
> is so expressive and impressive]
2. JavaScript data type detection
1. The universal typeof
Let’s first test how to obtain simple data types through typeof. Don’t say anything, the code is king:
/ / Get the data type of variable obj
function getType(obj) {
return typeof (obj);
}
/*Constant get type*/
alert(getType(1)); //number
alert(getType("jeff wong")); //string
alert(getType(true)); //boolean
alert(getType(undefined)); //undefined
alert(getType(null)); //object
/*Variable get 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 In that way, through the typeof operator, the first four simple data types are completely expected, but typeof null returns object. It should be noted that null is the only value of the null type, but null is not an object, and a variable with a null value is not an object, so the null type cannot be correctly obtained directly through typeof. To correctly obtain simple data types, just add some improvements to getType:
function getType(obj) {
return (obj === null) ? "null" : typeof (obj);
}
Let’s try it next Complex data type object:
function Cat() {
}
Cat.prototype.CatchMouse = function () {
//do some thing
}
// Get the data type of 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 except Function (please note the case) is returned Function, whether it is the common built-in objects of JavaScript such as Object, String or Date, etc., or a custom function, all returned by typeof are all objects without exception. But for custom functions, we prefer to get its "true face" (in the example, Cat, not object), and obviously, typeof does not have this conversion processing capability.
2. Constructor, I want to say I love you loudly
Since the universal typeof sometimes has no solution, how do we judge whether a variable is a custom function instance? We know that all objects in JavaScript have a constructor attribute. This attribute can help us determine the object data type, especially for custom functions:
var obj = "jeff wong";
alert(obj.constructor == String); //true
obj = new Cat();
alert(obj.constructor == Cat); //true
However, you can also test it with the following code:
//alert(1.constructor); //Numeric constant error Numeric constant has 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 has no constructor attribute
none = undefined;
alert(obj.constructor); //undefined has no constructor attribute
Experiments have shown that numeric constants, null and undefined do not have constructor attributes.
At this point, will you be as happy as Lou Zhu and think it’s finally done? The following code may also be of some inspiration and excavation:
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
It turns out that constuctor is not so easy to use in the case of prototype chain inheritance. What to do?
3. Intuitive instanceof
Hey, please instanceof makes a grand appearance. Judging from its naming, it seems to be to obtain an instance of a certain object. I don’t know if this is the right way to understand it? Anyway, let’s improve the above code and test it first:
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 It’s understandable
Okay, regarding the data type detection of JavaScript, Louzhu has summarized it here. I hope someone with a heart can add more.