Lodash Type judge the source code
"hungry Big front Node. JS advanced tutorial" address: Https://github.com/ElemeFE/node-interview
Lodash Introduction : Lodash is currently a very popular JS tool library, It encapsulates a lot of JS commonly used tool method, in reading the source code, you will find that the codes are verbose, the official website said this is performance optimization, whether you believe it or not, I believe it anyway!
(1) NULL type
function IsNull (value) {
return value = = = null;
}
(2) Number Type
function Isnumber (value) {
Return typeof value = = ' number' | |
(isobjectlike (value) && basegettag (value) = = ' [object number]');
}
Carefully to the reunion asked: "i go, so long!" "yes, That's The length." Because the number type is to contain many kinds of situations inside;
For example: This is the case of new number (), typeof is an object
(3) String Type
function Isstring (value) {
Const Type = typeof value;
return type = = ' string' | | (type = = ' Object' && value ! = null && ! Array. IsArray (value) && gettag (value) = = ' [object String]';
}
And judging the number type, excluding the state of the new String (), you would say: Object.prototype.toString.call is a step, but this will affect performance, anyway, The official website is so explained;
(4) arguments
function isarguments (value) {
Return typeof value = = ' Object' && value !== null && Gettag ( Value) = = ' [object Arguments]';
}
(5) function
function isfunction (value) {
if (! IsObject (value)) {
Return false
}
Const Tag = Basegettag (value)
return tag = = ' [object Function]' | | tag = = ' [object asyncfunction]' | |
Tag = = ' [object generatorfunction]' | | tag = = ' [object Proxy]'
}
The primary method is to increase the judgment of the native METHODS.
(6) Boolean
function Isboolean (value) {
return value = = = True | | value = = = False | |
(isobjectlike (value) && basegettag (value) = = ' [object Boolean]')
}
Summary: in general, the type judgment is from typeof, Object.prototype.toString.call and value, 3 aspects to cooperate to judge;
"hungry Big front Node. JS Advanced Tutorial"-javascript Basic Problem-type judgment