鉴别JS数据类型的全套方法

8 篇文章 1 订阅

 

  ECMAScript 标准定义了 7 种数据类型:Boolean、Null、Undefined、Number、String、Symbol(ES6新增)和Object,除Object以外的那6种数据类型也被称为基本数据类型,另外还有Array、Function等复杂数据类型。本文介绍一般类型判断方法,最后总给一套全面的数据类型判断方法。

一、typeof

  typeof是一个一元运算符(不是一个函数方法),可以鉴别null以外的基本数据类型以及Object和Function。它的返回值是小写的字符串:

1

2

3

4

5

6

7

8

9

10

11

/**** typeof ****/

typeof 37 ;  //输出 "number"

typeof "aaa" ;  //输出 "string"

typeof undefined ;  //输出 "undefined"

typeof false;  //输出 "boolean"

typeof {a:1,b:2};  //输出 "object"

typeof function(){};  //输出 "function"

 

//它不能鉴别null和array

typeof null;  //输出 "object"

typeof [1,2];  //输出 "object"

二、Object.prototype.toString.call()

  完美鉴别基本数据类型及Object、Function、Array、Date、Math等等类型

1

2

3

4

5

6

7

8

/**** Object.prototype.toString.call ****/

Object.prototype.toString.call("aa"); //输出 "[object String]"

Object.prototype.toString.call(123); //输出 "[object Number]"

Object.prototype.toString.call(null); //输出 "[object Null]"

Object.prototype.toString.call(undefined); //输出 "[object Undefined]"

Object.prototype.toString.call([1,2,3]); //输出 "[object Array]"

Object.prototype.toString.call(function(){}); //输出 "[object Function]"

Object.prototype.toString.call({a:1,b:2}); //输出 "[object Object]"

三、其他判断方法

  Array.isArray()可以判断一个数据是否是数组类型。

  instanceof操作符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。某些情况下也能用来检测数据类型,慎用。

1

2

3

4

5

6

7

8

9

10

11

/**** isArray判断数组 ****/

var arrTmp = [1,2];

Array.isArray(arrTmp);  //输出true

 

/**** instanceof判断类型,不准确 ****/

var numTmp1 = 123;

var numTmp2 = new Number(123);

numTmp1 instanceof Number; //输出 false

numTmp2 instanceof Number; //输出 true

arrTmp instanceof Array; //输出 true

arrTmp instanceof Object; //输出 true

四、全套判断方法

  (下面这段代码挪自Anguar源码,稍加修整)

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

var toString = Object.prototype.toString;

 

function isUndefined(value) {

    return typeof value === 'undefined';

}

function isDefined(value) {

    return typeof value !== 'undefined';

}

function isObject(value) {

    return value !== null && typeof value === 'object';

}

function isString(value) {

    return typeof value === 'string';

}

function isNumber(value) {

    return typeof value === 'number';

}

function isDate(value) {

    return toString.call(value) === '[object Date]';

}

var isArray = Array.isArray;

function isFunction(value) {

    return typeof value === 'function';

}

function isRegExp(value) {

    return toString.call(value) === '[object RegExp]';

}

function isWindow(obj) {

    return obj && obj.window === obj;

}

function isFile(obj) {

    return toString.call(obj) === '[object File]';

}

function isFormData(obj) {

    return toString.call(obj) === '[object FormData]';

}

function isBoolean(value) {

    return typeof value === 'boolean';

}

function isPromiseLike(obj) {

    return obj && isFunction(obj.then);

}

function isElement(node) {

    return !!(node && (node.nodeName || (node.prop && node.attr && node.find)));

}

function isArrayLike(obj) {

    if (obj == null || isWindow(obj)) {

        return false;

    }

    var length = "length" in Object(obj) && obj.length;

    if (obj.nodeType === 1 && length) {

        return true;

    }

    return isString(obj) || isArray(obj) || length === 0 || typeof length === 'number' && length > 0 && (length - 1) in obj;

}

  

分类: JavaScript

标签: 数据angular类型

0

0

« 上一篇:JS数组与对象的遍历方法大全
» 下一篇:js map()处理数组和对象数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值