唯品秀前端博客
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
< script >
var a = 'string';
var b = 1;
var c = true;
var f = function () {
    return a;
}
var g = null
var h = undefined
var i = Symbol()
var j = new Array()
var k = new Date()
var l = new RegExp()
var m = new Object()
var n = new Map()
var e = new Error()

var index = 1
if (index === 1) {
    // typeof 能判断的七种类型,首字母小写  string number boolean function undefined symbol object
    console.log(typeof a)
    console.log(typeof b)
    console.log(typeof c)
    console.log(typeof f)
    console.log(typeof h)
    console.log(typeof i)
    console.log('--------')
    console.log(typeof g)
    console.log(typeof j)
    console.log(typeof k)
    console.log(typeof l)
    console.log(typeof m)
    console.log(typeof n)
    console.log(typeof e)

} else if (index === 2) {
    console.log(f instanceof Function)
    // instanceof 只能判断七种数据类型,不能判断null,首字母大写  Array Date RegExp Object Function Map
    console.log(a instanceof String)
    console.log(b instanceof Number)
    console.log(c instanceof Boolean)
    console.log(g instanceof Object)
    console.log(h instanceof Object)
    console.log(i instanceof Symbol)
    console.log('--------')
    console.log(j instanceof Array)
    console.log(k instanceof Date)
    console.log(l instanceof RegExp)
    console.log(m instanceof Object)
    console.log(f instanceof Function)
    console.log(n instanceof Map)
    console.log(e instanceof Error)
} else if (index === 3) {
    // constructor无法判断 undefined  null
    Math.floor(9).constructor === Number[1, 2, 3].constructor === Array
    true
        '11'.constructor === String
    true
    true.constructor === Boolean
    true
    var fn = function () {
        return 1
    }
    undefined
    fn.constructor === Function
    true
    Symbol().constructor === Symbol
    true
    new Array().constructor === Array
    true
    new Date().constructor === Date
    true
    new RegExp().constructor === RegExp
    true
    new Object().constructor === Object
    true
    new Map().constructor === Map
    true
    new Error().constructor === Error
    true
}

// 判断是否为空
function isNull(obj) {
    return typeof obj === 'object' && !(obj instanceof Object) && obj === null
}
// 推荐使用 万能数据类型判断
function getObjectType(obj) {
    return Object.prototype.toString.call(obj).slice(8, -1);
}
console.log(getObjectType(n))
</script>
本站所有文章、图片、资源等如无特殊说明或标注,均为来自互联网或者站长原创,版权归原作者所有;仅作为个人学习、研究以及欣赏!如若本站内容侵犯了原著者的合法权益,可联系我们进行处理,邮箱:343049466@qq.com
赞(2) 打赏

上一篇:

下一篇:

相关推荐

0 条评论关于"原生javascript数据类型判断方法汇总集合"

表情

最新评论

    暂无留言哦~~
谢谢你请我吃鸡腿*^_^*

支付宝扫一扫打赏

微信扫一扫打赏