JS Learning Summary----Data type detection four ways (reprint)

Source: Internet
Author: User
Tags instance method object object

1. typeof operator to detect data types 

Console.log (typeof)//number

  Using typeof to detect data types, the first thing to return is a string, followed by the corresponding data type in the string

Example: "Number", "string", "Boolean", "Undefined","function","Object"

Console.log (typeof typeof function () {})//string

Limitations:

typeof null, "Object"

Cannot specify whether the subdivision is an array or a regular, or another value in the object, because using typeof to detect the data type and, for the value in the object data type, the last result returned is "object"

2. instanceof detects if an instance belongs to a class

var obj = [12,23];        Console.log (obj instanceof Array);

  Limitations:

For the basic data type, the result created by the literal method and the result created by the instance method is somewhat different. Strictly speaking, only the result of the instance is the standard object data type value, is also the standard number of this class of an instance, the result of the creation of the literal method is the basic data type value, not a rigorous instance, but because of the loose characteristics of JS, Results in a method that can be used on Number.prototype

1), can not be used to detect and process the literal method created by the basic data type value

Console.log (1 instanceof number),//false        Console.log (new number (1) instanceof number)//true

  2), instanceof characteristics: As long as the current instance of the prototype chain, we use its detection of the result is true

var ary = [];        Console.log (ary instanceof Array);//true        console.log (ary instanceof Object);//true        function fn () {        }        console.log (FN instanceof Function);//true        console.log (fn instanceof Object);//true  

3. Constructor constructor function and instanceof are very similar constructor can handle detection of basic data types

Constructor detection object is not the same as instanceof, normally it can't be detected.

var obj = [];        Console.log (Obj.constructor = = = Array)//true        var num = 1;        Console.log (Num.constructor = = number)//true

  Limitations: We can rewrite the prototype of the class, in the process of rewriting it is very likely that the previous constructor to cover out, so that the results are not accurate detection.

  For special data types, null and undefined, the classes they belong to are null and undefined, but the browser protects the two classes, not allowing us to access them outside

4, Object.prototype.toString.call () the most accurate and most common way of all types can be detected (basic and reference)

First get the ToString method on the object prototype, let the method execute, and change the point of the This keyword in the method

ToString's Understanding:

On the surface it should be converted to a string, but some ToString methods are not just converted to strings

The ToString method on number, string, Boolean, Array, RegExp, Date, and function prototypes is the type that converts the current data type to a string (their function is simply to convert to a string)

Object.prototype.toString () is not used to convert to a string, his role is to return the details of the class to which the current method execution principal (this in the method) belongs.  

({name: "John Doe"}). ToString ()//[object Object]        math.tostring ()//[object Math]
({name: "John Doe"}). ToString ()//[object Object]        math.tostring ()//[object Math]        var obj = {name: "Zhang San"};        This in Console.log (Obj.tostring ())//tostring is obj, which returns information about the class to which obj belongs. [Object Object] The first object represents the current instance is the object data type (this is fixed), the second object represents the class that obj belongs to is an object

Console.log ((1). toString ())//"1" Number.prototype.toString converted to string

Console.log (+) toString (2/8/10) converts the number to 2-, 8-, 10-binary

So the detection of the above method is as follows 

var ary = [];        Console.log (Object.prototype.toString.call (ary))//[object Array]

JS Learning Summary----Data type detection four ways (reprint)

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.