数据类型判断(包含各种对象属性)
来源:互联网 发布:java gbk 乱码 编辑:程序博客网 时间:2024/04/29 06:36
来自loadsh的封装,补充了NaN类型的判断。
const toString = Object.prototype.toString;function kindOf(val) { // 原始数据类型 if(typeof val === 'undefined') { return 'undefined'; } if(val === null) { return 'null'; } if(val === true || val === false || val instanceof Boolean) { return 'boolean'; } if(typeof val === 'string' || val instanceof String) { return 'string'; } if(typeof val === 'number' || val instanceof Number) { return val === val ? 'number' : 0 / 0; } // 函数 if(typeof val === 'function' || val instanceof Function) { return 'function'; } // 数组 if(typeof Array.isArray !== 'undefined' && Array.isArray(val)) { return 'array'; } //转化为字符串之前判断对象是否为Date【日期对象】与RegExp【正则表达式】的实例对象 if(val instanceof RegExp) { return 'regexp'; } if(val instanceof Date) { return 'date'; } // 其他对象 var type = toString.call(val); if(type === '[object RegExp]') { return 'regexp'; } if(type === '[object Date]') { return 'date'; } if(type === '[object Arguments]') { return 'arguments'; } if(type === '[object Error]') { return 'error'; } // es6对象: Map, WeakMap, Set, WeakSet if(type === '[object Set]') { return 'set'; } if(type === '[object WeakSet]') { return 'weakset'; } if(type === '[object Map]') { return 'map'; } if(type === '[object WeakMap]') { return 'weakmap'; } if(type === '[object Symbol]') { return 'symbol'; } // 数组类型 if(type === '[object Int8Array]') { return 'int8array'; } if(type === '[object Uint8Array]') { return 'uint8array'; } if(type === '[object Uint8ClampedArray]') { return 'uint8clampedarray'; } if(type === '[object Int16Array]') { return 'int16array'; } if(type === '[object Uint16Array]') { return 'uint16array'; } if(type === '[object Int32Array]') { return 'int32array'; } if(type === '[object Uint32Array]') { return 'uint32array'; } if(type === '[object Float32Array]') { return 'float32array'; } if(type === '[object Float64Array]') { return 'float64array'; } // 必定是个简单对象 return 'object';};
END
阅读全文
1 0
- 数据类型判断(包含各种对象属性)
- Python中如何判断对象是否包含某个属性?
- js 判断各种数据类型
- js 判断各种数据类型
- js 判断各种数据类型
- js 判断各种数据类型
- js 判断各种数据类型
- js 判断各种数据类型
- JavaScript 判断各种数据类型
- javascript各种数据类型判断方法
- js 判断对象数组中是否包含某个属性的值和对象转化成数组
- javascript 判断是否为数组或对象或各种其它数据类型
- javascript 判断是否为数组或对象或各种其它数据类型
- 属性中可以包含对象
- JavaScript判断对象数组是否包含对象
- js 判断各种数据类型的简单方法
- 【java】判断各种数据类型是否为空
- GDI+中图形对象包含关系判断
- 那些吓人的 Linux 命令
- 运行项目时候报错com.android.dx.command.Main with arguments {--dex --num-threads=4 --multi-dex --main-dex-lis
- 数字三角形
- 马哥Linux运维面授班27期开班典礼暨四大课程开始招生!
- IT领域的三大定律
- 数据类型判断(包含各种对象属性)
- [实时渲染] 3.2 可编程着色阶段
- 给一个新手程序员的建议
- java IO 之 DataStream 操作基本数据流对象
- TabLayout自定义样式
- Solr实现全文搜索 linux
- 传统JDBC开发(二)----抽象自己的工具类
- 菱形的输入方法
- 三位整数的个位,十位,百位数字之和。