数据类型相关操作集合 vjspUtils.types
数据类型相关操作集合,包括数据类型判断和转换
获取一个变量的类型
(static).getType(val) → {String}
示例
//return "string"
vjspUtils.types.getType('this is a string')
参数:
Name | Type | Description |
---|---|---|
val | Any | 待检测变量 |
返回: val的类型名称
Type String
判断指定值是否是数组
(static).isArray(val) → {Boolean}
示例
//return true
vjspUtils.types.isArray([])
参数:
Name | Type | Description |
---|---|---|
val | Any | 待判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否是函数
(static).isFunction(val) → {Boolean}
示例
//return true
vjspUtils.types.isFunction(function(){})
参数:
Name | Type | Description |
---|---|---|
val | Any | 待判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否是对象
(static).isObject(val) → {Boolean}
示例
function A(){};
var a = new A();
//return true;
vjspUtils.types.isFunction(A);
参数:
Name | Type | Description |
---|---|---|
val | Any | 待判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否是简单对象
(static).isPlainObject(val) → {Boolean}
{}或者new Object
示例
//return true
vjspUtils.types.isPlainObject({key:'value'});
参数:
Name | Type | Description |
---|---|---|
val | Any | 待判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否是数字
(static).isNumeric(val) → {Boolean}
(或者能转换成数字)
示例
//return true
vjspUtils.types.isNumeric(123);
//return true
vjspUtils.types.isNumeric('456');
参数:
Name | Type | Description |
---|---|---|
val | Any | 待判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否是数字
(static).isNumber(val) → {Boolean}
(从字符角度判断每个字符是否是数字)
参数:
Name | Type | Description |
---|---|---|
val | Any | 待判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否是整数
(static).isInteger(val) → {Boolean}
示例
//return true
vjspUtils.types.isInteger(123);
参数:
Name | Type | Description |
---|---|---|
val | Any | 待判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否是浮点数
(static).isDecimals(val) → {Boolean}
示例
//return true
vjspUtils.types.isDecimals(123.456);
参数:
Name | Type | Description |
---|---|---|
val | Any | 待判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否为html元素
(static).isHtmlElement(val) → {Boolean}
示例
//return true
vjspUtils.types.isHtmlElement(document.body);
参数:
Name | Type | Description |
---|---|---|
val | Object | 要判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否为RegExp对象
(static).isRegExp(val) → {Boolean}
示例
//return true;
vjspUtils.types.isRegExp(/\w+/ig);
参数:
Name | Type | Description |
---|---|---|
val | Object | 要判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否为NaN
(static).isNaN(val) → {Boolean}
示例
//return false
vjspUtils.types.isNaN(123);
参数:
Name | Type | Description |
---|---|---|
val | Object | 要判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否为null
(static).isNull(val) → {Boolean}
示例
//return true
vjspUtils.types.isNull(null);
参数:
Name | Type | Description |
---|---|---|
val | Object | 要判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否为undefined
(static).isUndefined(val) → {Boolean}
示例
//return true
vjspUtils.types.isUndefined(undefined);
参数:
Name | Type | Description |
---|---|---|
val | Object | 要判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否为Date对象
(static).isDate(val) → {Boolean}
示例
//return true
vjspUtils.types.isDate(new Date());
参数:
Name | Type | Description |
---|---|---|
val | Object | 要判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否为String
(static).isString(val) → {Boolean}
示例
//return true
vjspUtils.types.isString('this is a string');
参数:
Name | Type | Description |
---|---|---|
val | Object | 要判断的值 |
返回: 判断结果
Type Boolean
判断指定值是否为jQuery对象
(static).isjQueryObject(val) → {Boolean}
示例
//return true;
vjspUtils.types.isjQueryObject(jQuery('body'));
参数:
Name | Type | Description |
---|---|---|
val | Object | 要判断的值 |
返回: 判断结果
Type Boolean
字符串转换成数字
(static) toNumber(val, defaultValueopt) → {Number}
示例
//return 123
vjspUtils.types.toNumber('123');
参数:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
val | String | 待转换字符串 | ||
defaultValue | Number | optional | null | 转换失败返回的默认值 |
返回: 转后的数字
Type Number
字符串转换成整数
(static) toInteger(val, defaultValueopt) → {Integer}
示例
//return 123
vjspUtils.types.toInteger('123.45');
参数:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
val | String | 待转换字符串 | ||
defaultValue | Integer | optional | null | 转换失败返回的默认值 |
返回: 转后的数字
Type Integer
字符串转换成小数
(static) toFloat(val, defaultValueopt) → {Float}
示例
//return 123.45
vjspUtils.types.toFloat('123.45');
参数:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
val | String | 待转换字符串 | ||
defaultValue | Float | optional | null | 转换失败返回的默认值 |
返回: 转后的数字
Type Float
转换成布尔型
(static).toBoolean(val) → {Boolean}
字符串的“true”和“false”按字面值返回,其他的直接返回强制类型转换的结果
示例
//return false
vjspUtils.types.toBoolean('false');
参数:
Name | Type | Description |
---|---|---|
val | Any | 待转换的值 |
返回: 转换后的值
Type Boolean
转换成Date类型
(static) toDate(val, defaultValueopt) → {Date}
示例
//return a Date Object toString() is Tue Jan 10 2017 12:10:33 GMT+0800(中国标准时间)
vjspUtils.types.toDate('2017-01-10 12:10:33');
参数:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
val | String|Integer | 符合日期格式的字符串或者时间戳数字 | ||
defaultValue | Date | optional | null | 转换失败返回的默认值 |
返回: 转换后的日期
Type Date