zoukankan      html  css  js  c++  java
  • 判断js中的数据类型的几种方法

    判断js中的数据类型一般使用以下几种方法:typeof、instanceof、 Object.prototype.toString

    先举几个例子

    // 普通数据类型
    var str = "132456465";
    var num = 123;
    var bool = true;
    var nu = null;
    var un = undefined;
    var sym = Symbol(42);
    
    // 引用数据类型
    var obj = {
        hello: "你好哈喽"
    };
    var arr = ["1","c",5];
    var map = new Map([[1,2],[2,5]]);
    var set = new Set([1, 2, 3, 4, 5]);
    var date = new Date();
    var regexp = new RegExp('\w+');
    var math= Math.random();
    var fun = function () {
        console.log("这是函数");
    };
    function* gen() { 
      yield 1;
      yield 2;
      yield 3;
    };
    var pro = new Promise((res)=>{res("你好")})
    var weakmap = new WeakMap([[{}, 2], [{}, 5]]); 

    1、最常见的判断方法:typeof 

      typeof 只能判读除null以外的基础数据类型,无法判断除function以外引用数据类型

    console.log("-typeof-start-");
    console.log(typeof str == "string"); // true
    console.log(typeof num == "number"); // true
    console.log(typeof bool == "boolean"); // true
    console.log(typeof nu); // object
    console.log(typeof un == "undefined"); // true
    console.log(typeof sym == "symbol"); // true
    console.log(typeof obj); // object
    console.log(typeof arr); // object
    console.log(typeof map); // object
    console.log(typeof set); // object
    console.log(typeof date); // object
    console.log(typeof regexp); // object
    console.log(typeof math); // number
    console.log(typeof fun); // function
    console.log(typeof gen); // function
    console.log(typeof pro); // object
    console.log(typeof weakmap); // object
    //另外typeof 可以判断string、number、boolean、undefined的类型,其他还需要进一步判读
    console.log("-typeof-end-");

    2、判断已知数据是引用数据类型的方法: instanceof

    但是纯object不能使用instanceof验证,因为其他的类型object也是object
    console.log("-instanceof-start-");
    console.log(str instanceof String); // false
    console.log(num instanceof Number); // false
    console.log(bool instanceof Boolean); // false
    // console.log(nu instanceof null); // 报错
    // console.log(un instanceof undefined); // 报错
    console.log( sym instanceof Symbol); // true
    console.log( obj instanceof Object); // true
    console.log( arr instanceof Object); // true
    console.log( arr instanceof Array); // true
    console.log( map instanceof Map); // true
    console.log( set instanceof Set); // true
    console.log( date instanceof Date); // true
    console.log( regexp instanceof RegExp); // true
    console.log( math instanceof Number); // false
    console.log( fun instanceof Function); // true
    console.log( gen instanceof Function); // true
    console.log( pro instanceof Promise); // true
    console.log( weakmap instanceof WeakMap); // true
    // instanceof几乎可以验证所有的object 但是纯object不能使用instanceof验证,因为其他的类型object也是object
    console.log("-instanceof-end-");

    3、非常通用,不存在死角的判断方法: Object.prototype.toString

      推荐使用这种方法封装判读方法作为util.js中的方法console.log("-Object.prototype.toString-start-");var toStr = Object.prototype.toString;

    console.log(Object.prototype.toString.call(str)); // [object String]
    console.log(Object.prototype.toString.call(str) == "[object String]"); // true
    console.log(Object.prototype.toString.call(num)); // [object Number]
    console.log(Object.prototype.toString.call(bool)); // [object Boolean]
    console.log(Object.prototype.toString.call(nu)); // [object Null]
    console.log(Object.prototype.toString.call(un)); // [object Undefined]
    console.log(Object.prototype.toString.call(sym)); // [object Symbol]
    console.log(Object.prototype.toString.call(obj)); // [object Object] console.log(Object.prototype.toString.call(arr)); // [object Array] console.log(Object.prototype.toString.call(map)); // [object Map] console.log(Object.prototype.toString.call(set)); // [object Set] console.log(Object.prototype.toString.call(date)); // [object Date] console.log(Object.prototype.toString.call(regexp)); // [object RegExp] console.log(Object.prototype.toString.call(math)); // [object Number] console.log(Object.prototype.toString.call(fun)); // [object Function] console.log(Object.prototype.toString.call(gen)); // [object GeneratorFunction] console.log(Object.prototype.toString.call(pro)); // [object Promise] console.log(Object.prototype.toString.call(weakmap)); // [object WeakMap] console.log("-Object.prototype.toString-end-");
    function isString(val){
        return Object.prototype.toString.call(val) == "[object String]"
    };
    function isNumber(val){
        return Object.prototype.toString.call(val) == "[object Number]"
    };
    function isBoolean(val){
        return Object.prototype.toString.call(val) == "[object Boolean]"
    };
    function isNull(val){
        return Object.prototype.toString.call(val) == "[object Null]"
    };
    function isUndefined(val){
        return Object.prototype.toString.call(val) == "[object Undefined]"
    };
    function isSymbol(val){
        return Object.prototype.toString.call(val) == "[object Symbol]"
    };
    function isPlainObject(val){
        return Object.prototype.toString.call(val) == "[object Object]"
    };
    function isArray(val){
        return Object.prototype.toString.call(val) == "[object Array]"
    };
    function isMap(val){
        return Object.prototype.toString.call(val) == "[object Map]"
    };
    function isSet(val){
        return Object.prototype.toString.call(val) == "[object Set]"
    };
    function isDate(val){
        return Object.prototype.toString.call(val) == "[object Date]"
    };
    function isRegExp(val){
        return Object.prototype.toString.call(val) == "[object RegExp]"
    };
    function isFunction(val){
        return Object.prototype.toString.call(val) == "[object Function]"
    };
    function isGenerator(val){
        return Object.prototype.toString.call(val) == "[object GeneratorFunction]"
    };
    function isPromise(val){
        return Object.prototype.toString.call(val) == "[object Promise]"
    };
    function isWeakMap(val){
        return Object.prototype.toString.call(val) == "[object WeakMap]"
    };

    参考博客https://www.cnblogs.com/dushao/p/5999563.html

  • 相关阅读:
    编写程序,验证string是ipV4地址
    TCP三次握手和四次挥手
    链表和数组的区别
    cookie和session的区别
    GET和POST的区别
    TCP和UDP的区别
    java HashMap和Hashtable的区别
    java 堆和栈的区别
    最小栈的实现
    关于几个位运算的算法分析
  • 原文地址:https://www.cnblogs.com/aidixie/p/12818573.html
Copyright © 2011-2022 走看看