We are easily attracted by beautiful code and add these to our code base unknowingly. But I haven’t calmly thought about their advantages and disadvantages. No, I have collected a series of boolean functions that can be judged in the form of "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; }
Among the above isXX series, isUndefined is used the most in class libraries. For example, determine whether a certain parameter is passed in, determine whether the object has a certain attribute, etc. But this function doesn't have to exist, I've removed it. The reason is as follows
isUndefined and using congruence (===) or typeof add one more layer of function calls. Obviously, one more layer of function calls will be less efficient than using the native operator directly (although it is somewhat trivial), but it is still obvious if isUndefined is called many times, such as tens of thousands of times. I once added this function to the mailbox framework, and it was called more than 4,000 times. According to the performance analysis tool, it took up nearly 1% of the time. Just one judgment accounting for 1% of the call time is still terrible. Of course, isUndefined in the mailbox framework is at the top of multi-layer closures, and accessing it will also take more time. If this is not enough to make you give up isUndefined, please see below.
Functions encapsulate and abstract some code to a certain extent. 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, there is no need to encapsulate and extract a function.
isUndefined(a) doesn't save a few bytes compared to a === undefined (heh, you can make the name shorter but lose readability).
To sum up, I removed isNull/isUndefined/isBoolean/isNumber/isString for basic type judgment in the class library. When these judgments are needed, typeof operators are used directly.
The above is the content of the judgment method of each variable type in JavaScript. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!