javascript检测数据类型

代码如下:

(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 $;
}));

测试如下:

《javascript检测数据类型》 Paste_Image.png

    原文作者:wengjq
    原文地址: https://www.jianshu.com/p/59e2328b0e92
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞