Data type detection js

       Lift the first type of data is detected most people think should be the typeof 'xxx' == 'data type' calm this method for basic data types of detection is very convenient, but when faced with a reference data type Object Shique helpless, below detection Laijiangjiang on these data types it
  

  1. Using typeof method

    First we look at the following example will print out what the console

//检测方法                                    //输出类型  
typeof
function(){} ====> ‘functiontypeof "" ====> 'string' typeof 545 ====> 'number' typeof null ====> 'object'
typeof true ====> 'boolean' typeof undefined ====> 'undefined' typeof [] ====> 'object' typeof {} ====> 'object'
typeof Symbol() ====> 'symbol'

       In the above example, we can clearly see that using    typeof    on the above-mentioned defects can not be detected when detecting the type of do array null data such as types, as will be typeof why why the null data types as the basic object detected as (this is the problem of the historical legacy js) - initial implementation in JavaScript, JavaScript values are represented by a label indicating the type of data and the actual value. Label object type is 0. Since  null represents null pointer (most internet value 0x00), therefore, has become null tag type 0, typeof nullit returns false "object" , so as the basic data types typeof method of simple data types other than the null Analyzing It is also a good program

 

  2. The method of determining the data type instanceof

    Under the first instanceof mainly when used to doing it in mdn we can see on the official website of interpretation instanceof
    instanceofoperator is used to detect  constructor.prototype whether there is a parameter  object on the prototype chain, which means that as long as the constructor in the prototype object on the prototype chain values are the object type or array type (prototype chain is traversed in principle determination)
       FIG:
    an array of objects ----- ----

                     

       所以可以知道,该方法用来判断引用类型下的对象具体类型是不错的一种方法,这种方法在判断除了Function 的基本类型 之外 全部会返回false,其中 function 的原型链上也是继承至object,所以 function  instanceof Object 也是返回true 的 即可以用来判断除 object本身判断之外的引用类型,以及function基本类型 ,和原型继承下来的方法都可以判断出来

[] instanceof Array                        ====>         true
[] instanceof object                       ====>         true
({}) instanceof Array                      ====>         false
'1324' instanceof String                   ====>         false
(function(){}) instanceof Function         ====>         true
 2313 instanceof Number                    ====>         false
function test(ang) { this.a = ang; } var mytest = new test("Honda"); var a = mytest instanceof Car; ====> true

 

 

   3.使用 Object.prototype.toString.call() 方法  目前来说这个应该算是万金油类型的能满足大多数判断(当然以为该方法基于原型 如果原型上的toString被修改 这方法自然也就不准了),

当然这方法是无法判断除原型上的继承实例的 如要判断需使用 instanceof 方法
  

Object.prototype.toString.call(null);              =======>       ”[object Null]”
Object.prototype.toString.call(undefined); =======> ”[object Undefined]”
Object.prototype.toString.call(“abc”); =======> ”[object String]”
Object.prototype.toString.call(123); =======> ”[object Number]”
Object.prototype.toString.call(true); =======> ”[object Boolean]”
Object.prototype.toString.call(function);          =======>       ”[object Function]”
Object.prototype.toString.call([]);                =======>       ”[object Array]”
Object.prototype.toString.call({});                =======>       ”[object Object]”
 

 

 

 

 

Guess you like

Origin www.cnblogs.com/jjq-exchange/p/11356882.html