javascript检测数据类型

作者: wengjq | 来源:发表于2016-12-15 10:42 被阅读90次

代码如下:

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
       define([], factory);
   }else if (typeof exports === 'object') {
       module.exports = factory();
   }else {
       root.$ = factory();
   }
}(this, function () {
   'use strict';

    var $ = {};
    var types = 'Array Object String Date RegExp Function Boolean Number Null Undefined'.split(' ');
    function type() {
        return Object.prototype.toString.call(this).slice(8, -1);
    }
    for (var i = types.length; i--;) {
         $['is' + types[i]] = (function (self) {
          return function (elem) {
                return type.call(elem) === self;
          };
        })(types[i]);
  }
    return $;
}));

测试如下:

Paste_Image.png

相关文章

网友评论

    本文标题:javascript检测数据类型

    本文链接:https://www.haomeiwen.com/subject/wkwwmttx.html