首页 > 语言 > JavaScript > 正文

JS开发常用工具函数(小结)

2024-05-06 15:37:09
字体:
来源:转载
供稿:网友

1、isStatic:检测数据是不是除了symbol外的原始数据

function isStatic(value) {  return(    typeof value === 'string' ||    typeof value === 'number' ||    typeof value === 'boolean' ||    typeof value === 'undefined' ||    value === null  )}

2、isPrimitive:检测数据是不是原始数据

function isPrimitive(value) {  return isStatic(value) || typeof value === 'symbol'}

3、isObject:判断数据是不是引用类型的数据 (例如: arrays, functions, objects, regexes, new Number(0),以及 new String(''))

function isObject(value) {   let type = typeof value;   return value != null && (type == 'object' || type == 'function');}

4、isObjectLike:检查 value 是否是 类对象。 如果一个值是类对象,那么它不应该是 null,而且 typeof 后的结果是 "object"

function isObjectLike(value) {   return value != null && typeof value == 'object';}

5、getRawType:获取数据类型,返回结果为 Number、String、Object、Array等

function getRawType(value) {  return Object.prototype.toString.call(value).slice(8, -1)}//getoRawType([]) ==> Array

6、isPlainObject:判断数据是不是Object类型的数据

function isPlainObject(obj) {  return Object.prototype.toString.call(obj) === '[object Object]'}

7、isArray:判断数据是不是数组类型的数据

function isArray(arr) {  return Object.prototype.toString.call(arr) === '[object Array]'}

将isArray挂载到Array上

Array.isArray = Array.isArray || isArray;

8、isRegExp:判断数据是不是正则对象

function isRegExp(value) {  return Object.prototype.toString.call(value) === '[object RegExp]'}

9、isDate:判断数据是不是时间对象

function isDate(value) {  return Object.prototype.toString.call(value) === '[object Date]'}

10、isNative:判断 value 是不是浏览器内置函数

内置函数toString后的主体代码块为 [native code] ,而非内置函数则为相关代码,所以非内置函数可以进行拷贝(toString后掐头去尾再由Function转)

function isNative(value) {  return typeof value === 'function' && /native code/.test(value.toString())}

11、isFunction:检查 value 是不是函数

function isFunction(value) {  return Object.prototype.toString.call(value) === '[object Function]'}

12、isLength:检查 value 是否为有效的类数组长度

function isLength(value) {   return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= Number.MAX_SAFE_INTEGER;}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选