Javascript data type detection

Source: Internet
Author: User

1) when detecting JavaScript types, the typeof operator is usually used, and a string that identifies the number of operations is returned. For any variable, the return result using the typeof operator is of only 6 types.

1. Number 2, string 3, Boolean 4, object 5, function 6, and undefined. However, when you try to use typeof to detect null values, the returned object is not suitable. You may think that null should be of no type, therefore, most people are doing a method to determine the null type.

function type(oo){   return (oo ==null) ? "null":(typeof oo);}

2) but typeof cannot detect complex data types, such as expression objects, date objects, and mathematical objects.

It can only return one object type, and does not know the specific date or something,

In JavaScript, the constructor attribute is used to determine the data type.

VaR value = underfined;

Alert (value. constructor );

However, the direct numeric value cannot be used in this way. You need to add a parentheses. In this case, the parentheses operator can directly convert the numeric value into an object, a bit like the integer and INT types in Java,

Alert (30). constructor)

3) The tostring () method can also be used to detect object types and is the most accurate. Generally, the object is converted into a string, then, check whether the string contains the standard characters specific to the array,

The format of the returned string is as follows:

[Object calss]

If the object type is generic, the class indicates the internal type of the object. The name of the internal type corresponds to the constructor name of the object. For example, the class of the array object is array, class "Date" of the date object"

The specific statement is as follows,

   var date = new Date();   alert(typeof date);   var m=Object.prototype.toString;   alert(m.apply(date));

Provides a common type detection code

Function Type (o ){

VaR _ tostring = object. Prototype. tostring;

VaR _ type = {

"Underfined": "underfined ",

"Number": "Number ",

"Boolean": "Boolean ",

"String": "string ",

''[Object function]": "function ",

''[Object Regexp]": "Regexp ",

''[Object array]": "array ",

''[Object date]": "date ",

''[Object Error]": "error"

}

Return _ type [typeof o] | _ type [_ tostring. Call (o)] | (O? "Object": "null ")

}

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.