zoukankan      html  css  js  c++  java
  • JavaScript测试数据类型的三种方法

    typeof


    console.log(typeof a);    //undefined
    
    console.log(typeof(true));  //boolean
    
    console.log(typeof '111');  //string
    
    console.log(typeof 111);   //number
    
    console.log(typeof NaN);   //number
    
    console.log(typeof null);  //object
    
    
    var str = new String();
    
    console.log(typeof(str));    //object
    
     
    var  fn = function(){};
    
    console.log(typeof(fn));  //function
    
     
    class Box{
    
      constructor(){
    
      }
    
    }
    
    var box = new Box();
    
    console.log(typeof box);  //object
    

    Object.prototype.toString.call();


    console.log(Object.prototype.toString.call("aaa"));  //[object String]
     
    console.log(Object.prototype.toString.call(111));  //[object Number]
     
    console.log(Object.prototype.toString.call(true));  //[object Boolean]
     
    console.log(Object.prototype.toString.call(undefined));  //[object Undefined]
     
    console.log(Object.prototype.toString.call(null));  //[object Null]
     
    console.log(Object.prototype.toString.call({name: "zhang"}));  //[object Object]
     
    console.log(Object.prototype.toString.call(function(){}));  //[object Function]
     
    console.log(Object.prototype.toString.call([]));  //[object Array]
     
    console.log(Object.prototype.toString.call(new Date));  //[object Date]
     
    console.log(Object.prototype.toString.call(/a/));  //[object RegExp]
    
    class Test{
      constructor(){
    
      }
    }
    var test = new Test();
    console.log(Object.prototype.toString.call(test));  //[object Object]
    

    constructor


    console.log((111).constructor.name)   //Number
    
    console.log(("111").constructor.name)    //String
    
    console.log((aaa).constructor.name)   //aaa is not defined
    
    console.log((/a/).constructor.name)   //RegExp
    
    console.log((true).constructor.name)   //Boolean
    
    console.log(({name:"zhang"}).constructor.name)   //Object
    
    console.log((new Date).constructor.name)   //Date
    
    console.log(([]).constructor.name)   //Array
    
    console.log((function(){}).constructor.name)   //Function
    
    class Box{
      constructor(){
    
      }
    }
    var test = new Box();
    console.log(test.constructor.name)   //Box
    
  • 相关阅读:
    在sed中怎么样产生控制字符^M
    递归遍历目录的shell脚本
    递归法遍历目录查找文件
    【30.01%】【hdu 3397】Sequence operation
    【23.68%】【hdu 2871】Memory Control
    【33.18%】【hdu 5877】Weak Pair (3种解法)
    【38.96%】【hdu 1540】Tunnel Warfare
    【26.8%】【CF 46D】Parking Lot
    【35.20%】【CF 706D】Vasiliy's Multiset
    【33.20%】【LA 4320】【Ping pong】
  • 原文地址:https://www.cnblogs.com/JHJavaScript/p/11420770.html
Copyright © 2011-2022 走看看