最新文章:

您的位置: 富录-前端开发|web技术博客 > JavaScript > JavaScript判断变量数据类型的方法

JavaScript判断变量数据类型的方法

发布时间:2020年06月17日 评论数:抢沙发阅读数: 4103

    JavaScript

    数据类型

    var a = 123;
    var b = 'abc';
    var c =  true;
    var d = undefined;
    var e = null;
    var f = {};
    var g = [];
    var h = function(){};
    var i = /^[a-zA-Z]{5,20}$/;
    var j = new String('123');
    var k = new Object();
    var l = new Function();
    var m = new Array();
    var n = new RegExp();


    typeof()

    可以判断基本数据类型,但无法判断对象的具体类型
    console.log(typeof a); // number
    console.log(typeof b); // string
    console.log(typeof c); // boolean
    console.log(typeof d); // undefined
    console.log(typeof e); // object
    console.log(typeof f); // object
    console.log(typeof g); // object
    console.log(typeof h); // function
    console.log(typeof i); // object
    console.log(typeof j); // object
    console.log(typeof k); // object
    console.log(typeof l); // function
    console.log(typeof m); // object
    console.log(typeof n); // object


    Object.prototype.toString.call()

    可以判断具体的对象类型,包括正则等,但是无法判断自定义对象类型。
    console.log(Object.prototype.toString.call(a)); //[object Number] 
    console.log(Object.prototype.toString.call(b)); //[object String] 
    console.log(Object.prototype.toString.call(c)); //[object Boolean] 
    console.log(Object.prototype.toString.call(d)); //[object Undefined] 
    console.log(Object.prototype.toString.call(e)); //[object Null] 
    console.log(Object.prototype.toString.call(f)); //[object Object] 
    console.log(Object.prototype.toString.call(g)); //[object Array] 
    console.log(Object.prototype.toString.call(h)); //[object Function] 
    console.log(Object.prototype.toString.call(i)); //[object RegExp] 
    console.log(Object.prototype.toString.call(j)); //[object String] 
    console.log(Object.prototype.toString.call(k)); //[object Object] 
    console.log(Object.prototype.toString.call(l)); //[object Function] 
    console.log(Object.prototype.toString.call(m)); //[object Array] 
    console.log(Object.prototype.toString.call(n)); //[object RegExp] 


    instanceof

    只能判断对象的具体类型(包含自定义对象)
    console.log(a instanceof Number) // false
    console.log(b instanceof String) // false
    console.log(c instanceof Boolean) // false
    console.log(f instanceof Object) // true
    console.log(g instanceof Array) // true
    console.log(h instanceof Function) // true
    console.log(i instanceof RegExp) // true
    console.log(j instanceof String) // true
    console.log(k instanceof Object) // true
    console.log(l instanceof Function) // true
    console.log(m instanceof Array) // true
    console.log(n instanceof RegExp) // true
    function Test() {
        this.prop = 123;
    }
    var test = new Test()
    
    console.log(typeof test) // object
    console.log(Object.prototype.toString.call(test)) // [object Object]
    console.log(test instanceof Test) // true  test是Test的一个实例

二维码加载中...
本文作者:DGF      文章标题: JavaScript判断变量数据类型的方法
本文地址: http://arbays.com/post-161.html     本文已被百度收录!
版权声明:若无注明,本文皆为“富录-前端开发|web技术博客”原创,转载请保留文章出处。
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论