JS's judgment variable type __JS

Source: Internet
Author: User
OK, first write about JS variable type of judgment bar, found in the study of code principles or debugging bugs in the process often encounter this problem, although there are many such examples on the Internet, or from their
own to write a collection bar, it is convenient to use.
1. First of all, needless to say, must be typeof, it is not a function, so the following parameters do not need parenthesis;
2.instanceof, to see that it has a meaning in itself, yes, look at the type of the function that it belongs to, but use
3.object.prototype.tostring.call (o) to determine whether the object is the target type. Object number]
when using the method tostring method, the [[Class]] property of the variable o
is obtained, and [[Class]] is an internal property, and all objects (native and host objects) have this property. [ [Class]] A string value that indicates the type of the object.
believe that the first two kinds of people will often encounter, the third may not be so easy to use, and so we understand the prototype prototype and call methods will feel that the same as the normal function calls;
In addition, we can encapsulate some of their own methods;
    function GetType (o) { 
                var _t; return (_t = typeof (o)) = = 
                "Object"? O==null && "Null" | | Object.prototype.toString.call (o). Slice (8,-1): _t). toLowerCase (); 

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.