Javascript type judgment

Source: Internet
Author: User

Today I saw a blog by situ, which is a deep copy of JavaScript. It involves a method for determining the type. The analysis is as follows:

 

 VaR is = function (OBJ, type ){
VaR tostring = object. prototype. tostring, undefined; return OBJ = NULL & type = 'null' | OBJ = undefined & type = 'undefined' | tostring. call (OBJ ). slice (8,-1) ==== type;} // each logic and operation is enclosed in parentheses in the original text, but according to the operator priority, parentheses can be omitted // The first line declares undefined, to improve performance, you do not have to query undefined in the top-level scope.

 

 

As explained in the ECMA-262, object. Prototype. tostring () returns the type of the object instance in the format of "[object", class, and "]" string.

Therefore, slice is used to extract the 'class' value, that is, the type value.

Among them, null and undefined are exceptions, because they return

[Object object] in IE

Standard browser [Object window].

Therefore, let alone make a judgment.

 


 

 

 

VaR is = function (OBJ, type) {var tostring = object. prototype. tostring; return OBJ = NULL & type = 'null' | OBJ = undefined & type = 'undefined' | tostring. call (OBJ ). slice (8,-1) === type ;}

 

 

 

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.