"Front End Learning Note 01" javascript Source method for judging data types

Source: Internet
Author: User

primitive Type (value type): Undefined, Null, number, String, Boolean;

Object Type (reference type): Object;

typeof

The standard type can be recognized, null outside (return object), and no specific object type (function excepted) is recognized.

Examples of usage:

var num = 100;typeof num;  

Note: Except for number, String, Boolean, undefined, and function types, the remaining types are judged to be object (including null).

instanceof

You can identify built-in object types, custom object types, and cannot discriminate between primitive types.

var arr = [];arr instanceof Array; -->true
Object.prototype.toString.call ()

You can identify the standard type, built-in object type, and not the custom object type.

Object.prototype.toString.call (123); --[Object number]
Constructor

The built-in object types, custom object types, and standard types can be distinguished (but underfined/null not recognized).

var num = 100;num.constructor = = = number; --true;

Return constructor Syntax:

function Getconstructorname (obj) {    return (obj===undefined| | obj===null)? obj: (obj.constructor&&obj.constructor.tostring (). Match (/function\s* ([^ (]*)/) [1]);} Match () Gets the number in the returned function number () {[native code]}.

"Front End Learning Note 01" javascript Source method for judging data types

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.