dataType.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. *
  3. * @desc 数据类型判断
  4. * @param {Object} o
  5. * @param {Object} obj
  6. * @return {Boolean}
  7. */
  8. function dataType(o, type) {
  9. if (type) {
  10. var _type = type.toLowerCase();
  11. };
  12. switch (_type) {
  13. case 'string':
  14. return Object.prototype.toString.call(o) === '[object String]';
  15. case 'number':
  16. return Object.prototype.toString.call(o) === '[object Number]';
  17. case 'boolean':
  18. return Object.prototype.toString.call(o) === '[object Boolean]';
  19. case 'undefined':
  20. return Object.prototype.toString.call(o) === '[object Undefined]';
  21. case 'null':
  22. return Object.prototype.toString.call(o) === '[object Null]';
  23. case 'function':
  24. return Object.prototype.toString.call(o) === '[object Function]';
  25. case 'array':
  26. return Object.prototype.toString.call(o) === '[object Array]';
  27. case 'object':
  28. return Object.prototype.toString.call(o) === '[object Object]';
  29. case 'nan':
  30. return isNaN(o);
  31. case 'elements':
  32. return Object.prototype.toString.call(o).indexOf('HTML') !== -1;
  33. default:
  34. return Object.prototype.toString.call(o);
  35. };
  36. };
  37. module.exports = dataType;