Methods for determining various JavaScript variable types

Source: Internet
Author: User

It is easy for us to be attracted by beautiful code, and we can add this to our own code library without knowing it. But never calmly think about their advantages and disadvantages. No, I just collected a series of shapes like "is it ......? .

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_obj.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 false;    }    return true;}

In the isXX series above, isUndefined is used most frequently in class libraries. Such as determining whether a parameter is passed in and whether the object has a certain attribute. But this function does not have to exist. I have removed it. The reasons are as follows:

  1. IsUndefined and the use of full equals (=) or typeof has an additional layer of function calls. Obviously, multiple function calls are less efficient than using native operators (although insignificant), but it is obvious if isUndefined calls are called more than times. I used to add this function to my mailbox framework. It has been called more than 4000 times, and it takes nearly 1% of the time from the performance analysis tool. It is terrible to judge the call time that accounts for 1%. However, the isUndefined in the mailbox framework is at the top layer of the multi-layer closure, and the access will take a lot of time. If this is not enough for you to give up isUndefined, please refer to the following.
  2. To a certain extent, a function encapsulates and abstracts some code. It is one of the ways to organize good code and helps reduce the complexity of the Code. However, there is only one sentence in the isNull/isUndefined/isBoolean/isNumber/isString function, and the abstraction level is very low. Therefore, a function is extracted without encapsulation.
  3. IsUndefined (a) does not save several bytes compared to a = undefined (Oh, you can name it shorter but it loses readability ).

In summary, I removed isNull, isUndefined, isBoolean, isNumber, and isString for basic types in the class library, and directly used the typeof operator when using these judgments.

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.