JavaScript type judgment code Analysis _javascript Tips

Source: Internet
Author: User
Copy Code code as follows:

The 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;
}
The original text has parentheses wrapping each logic and operation, but according to operator precedence, parentheses can be omitted
The first line declares undefined, and personal understanding is to improve performance without having to go to the top-level scope to query undefined.


According to the interpretation in ECMA-262, Object.prototype.toString () returns the type of the object instance, returning the format "[Object", Class, and "]" string.
So the ' class ' value is intercepted by slice, which is the type value.
Where null and undefined are exceptions, because they return the
IE in [Object Object]
Standard Browser [object Window].
So alone come up with judgment.
Related article: JavaScript deep copy

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.