加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 教程 > 正文

JavaScript开发优秀的工具函数

发布时间:2021-05-24 23:00:37 所属栏目:教程 来源:互联网
导读:副标题#e# 1、isStatic:检测数据是不是除了symbol外的原始数据 functionisStatic(value){ return( typeofvalue===string|| typeofvalue===number|| typeofvalue===boolean|| typeofvalue===undefined|| value===null ) } 2、isPrimitive:检测数据是不是原
副标题[/!--empirenews.page--]

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上

ArrayArray.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) {  

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读