JavaScript data type judgment

Source: Internet
Author: User
JavaScript data is divided into two types: simple data and complex data. Simple data contains number 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.

Omnipotent typeof

Let's first test how to obtain simple data types through typeof. Let alone anything. The code is king:

// 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:

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

Next, try the complex data type object:

Function Cat () {} Cat. prototype. catchMouse = function () {// do some thing} // obtain the data type function getType (obj) {return (obj = null) of the variable obj )? "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.

Constructor: I want to say I love you

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:

var obj = "jeff wong";  alert(obj.constructor == String); //true  obj = new Cat();  alert(obj.constructor == Cat); //true  

However, you can test the following code:

// Alert (1. constructor); // numeric constant error digital constant without 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 no 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.

The following code may be somewhat enlightening and useful for mining:

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?

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:

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

Address of this article: http://www.nowamagic.net/librarys/veda/detail/531,welcome.

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.