The method of judging the types of JavaScript variables

Source: Internet
Author: User

It's easy to be attracted to beautiful code, and it's also an imperceptible addition to our code base. But did not calmly think of their merits and demerits. This is not, I have collected a series of shapes such as "Whether for ...?" "The Boolean function of the judgment.

isnull:function (a) {return a = = = Null;},isundefined:function (a) {return a = = = Undefined;},isnumber:function (a) {return typeof a = = ' number ';},isstring:function (a) {return typeof a = = = ' string ';}, Isboolean:function (a) {return typeof a = = ' Boolean ';},isprimitive:function (b) {var a = typeof B;return!! (b = = = Undefined | | b = = = NULL | | a = = ' Boolean ' | | a = = ' Number ' | | a = = ' string ');},isarray:function (a) {return proto_ob J.tostring.call (a) = = = ' [Object Array] ';},isfunction:function (a) {return Proto_obj.toString.call (a) = = = ' [Object Function] ';},isplainobject:function (o) {if (!o | | o = = Win | | o = = DOC | | o = = = Doc.body) {return false;} Return ' isprototypeof ' in O && proto_obj.toString.call (o) = = = ' [Object Object] ';},iswindow:function (o) {return o && typeof o = = = ' object ' && ' SetInterval ' in O;},isemptyobject:function (o) {for (Var a in O) {return FA    Lse } return true;} 

Of the above Isxx series, isundefined is used most in class libraries. If you decide whether a parameter is passed in, determine if the object has a property, and so on. But this function does not have to exist, I have removed it. For the following reasons

    1. isundefined a layer of function calls with the use of congruent (= = =) or typeof. It is clear that more layers of function calls are less efficient (albeit trivial) than using native operators directly, but it is obvious if the number of isundefined calls is a lot like tens of thousands of times. I have added this function to the mailbox framework for more than 4,000 calls, and it takes nearly 1% of the time from the profiling tool. A mere 1% call time is scary. Of course, the isundefined in the mailbox framework is at the top level of the multilayer closures, which can also take more time to access. If this is not enough for you to give up isundefined, take a look below.
    2. The function is to some extent the encapsulation of some code, abstract. is one way to organize good code, and it helps to reduce the complexity of your code. But there is only one sentence in the Isnull/isundefined/isboolean/isnumber/isstring function, and the level of abstraction is very low. So there is no need to encapsulate and extract a function altogether. Tahe County Xianqing Machinery
    3. Isundefined (a) does not save a few bytes compared to a = = = undefined (ah, you can name the shorter but lose readability).

In conclusion, I removed the isnull/isundefined/isboolean/isnumber/isstring of the basic type judgment in the class library and needed to use the TypeOf operator directly when using these judgments.

The method of judging the types of JavaScript variables

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.