"hungry Big front Node. JS Advanced Tutorial"-javascript Basic Problem-type judgment

Source: Internet
Author: User
Tags return tag

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

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.