• 在javaScript中检测数据类型的几种方式


    类型检测的方法

    1. typeof

    2. instanceof

    3. Object.protype.toString

    4. constructor

    5. duck type:鸭子类型

    typeof

    返回一个字符串,适合函数对象和基本类型的判断

    var a = 123;
    var b = '123';
    var c = false;
    var d = null;
    var e = undefined;
    var f = {};
    var g = function(){};
    var h = [];
    var i = NaN;
    
    console.log(typeof a);
    console.log(typeof b);
    console.log(typeof c);
    console.log(typeof d);
    console.log(typeof e);
    console.log(typeof f);
    console.log(typeof g);
    console.log(typeof h);
    console.log(typeof i);
    

    instanceof

    返回一个布尔值,判断对象类型(基于原型链的方式)

    原理:
    左边对象的原型链(_proto_原型)上是否有右边构造函数的prototype对象属性

        var a = 123;
        var b = '123';
        var c = false;
        var d = null;
        var e = undefined;
        var f = {};
        var g = function(){};
        var h = [];
        var i = NaN;
        console.log(a instanceof Number);
        console.log(b instanceof String);
        console.log(c instanceof Boolean);
        console.log(d instanceof Object);
        console.log(e instanceof Object);
        console.log(f instanceof Object);
        console.log(g instanceof Function);
        console.log(h instanceof Array);
        console.log(i instanceof Number);//NaN是基本类型

    这么多false就说明instanceof不适合用于基本数据类型!!

    Object.prototype.toString

    var oP = Object.prototype,
    toString = oP.toString;
    console.log(toString.call(123));//[object Number]
    console.log(toString.call('123'));//[object String]
    console.log(toString.call(false));//[object Boolean]
    console.log(toString.call(undefined));//[object Undefined]
    console.log(toString.call(null));//[object Null]
    console.log(toString.call({a: '123'}));//[object Object]
    console.log(toString.call(/123/));//[object RegExp]
    console.log(toString.call([123]));//[object Array]
    console.log(toString,call(Date));//[object Function]
    

    注意:IE6/7/8 Object.prototype.toString.call(null/undefined)返回的是[object Object]

    constructor

    var a = 123;
    var b = '123';
    var c = false;
    var d = null;
    var e = undefined;
    var f = {};
    var g = function(){};
    var h = [];
    var i = NaN;
    console.log(a.constructor == Number);
    console.log(b.constructor == String);
    console.log(c.constructor == Boolean);
    // console.log(d.constructor == Object);
    // console.log(e.constructor == Object);
    console.log(f.constructor == Object);
    console.log(g.constructor == Function);
    console.log(h.constructor == Array);
    console.log(i.constructor == Number);
  • 相关阅读:
    Centos下安装Redis
    BZOJ 4870 [Shoi2017]组合数问题 ——动态规划 矩阵乘法
    BZOJ 4868 [Shoi2017]期末考试 ——三分 枚举
    BZOJ 4584 [Apio2016]赛艇 ——动态规划
    BZOJ 2806 [Ctsc2012]Cheat ——后缀自动机 单调队列优化DP
    BZOJ 2330 [SCOI2011]糖果 ——差分约束系统 SPFA
    Topcoder SRMCards ——贪心
    CTSC 1999 家园 【网络流24题】星际转移
    BZOJ 3489 A simple rmq problem ——KD-Tree
    BZOJ 2733 [HNOI2012]永无乡 ——线段树 并查集
  • 原文地址:https://www.cnblogs.com/lguow/p/10746341.html
Copyright © 2020-2023  润新知