Javascript type judgment code analysis

Source: Internet
Author: User

Copy codeThe Code 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 brackets can be omitted Based on the operator priority.
// The first line declares undefined. In my personal understanding, 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.
Related Articles: javascript deep copy

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.