码迷,mamicode.com
首页 > Web开发 > 详细

js判断数据类型的封装函数以及判断函数。

时间:2020-07-02 14:51:56      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:function   OLE   函数对象   ase   reac   tostring   number   func   数据类型   

var type = function (o){
  var s = Object.prototype.toString.call(o);
  return s.match(/\[object (.*?)\]/)[1].toLowerCase();
};

[‘Null‘,
 ‘Undefined‘,
 ‘Object‘,
 ‘Array‘,
 ‘String‘,
 ‘Number‘,
 ‘Boolean‘,
 ‘Function‘,
 ‘RegExp‘
].forEach(function (t) {
  type[‘is‘ + t] = function (o) {
    return type(o) === t.toLowerCase();
  };
});    //给type函数对象封装各个判断的方法

type.isObject({}) // true
type.isNumber(NaN) // true
type.isRegExp(/abc/) // true

 

js判断数据类型的封装函数以及判断函数。

标签:function   OLE   函数对象   ase   reac   tostring   number   func   数据类型   

原文地址:https://www.cnblogs.com/sidianok/p/13224625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!