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
    
  • 相关阅读:
    hdu 5170 GTY's math problem(水,,数学,,)
    hdu 5178 pairs(BC第一题,,方法不止一种,,我用lower_bound那种。。。)
    hdu 5179 beautiful number(构造,,,,)
    cf12E Start of the season(构造,,,)
    cf12D Ball(MAP,排序,贪心思想)
    cf 12C Fruits(贪心【简单数学】)
    cf 12B Correct Solution?(贪心)
    hdu 5171 GTY's birthday gift(数学,矩阵快速幂)
    hdu 5172 GTY's gay friends(线段树最值)
    cf 11D A Simple Task(状压DP)
  • 原文地址:https://www.cnblogs.com/JHJavaScript/p/11420770.html
Copyright © 2011-2022 走看看