源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Js检查变量类型的代码()

  • 时间:2022-10-21 16:52 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Js检查变量类型的代码()
JavaScript检查变量的类型,并判断是整形或是字符串或是其它类型等等。
[Ctrl+A 全选 注:[url=http://www.1sucai.cn/article/23421.htm]如需引入外部Js需刷新才能执行[/url]]
2、toString 本来是用来做字符串转换的,不过现在流行用来做变量类型的检查了。舜子这里也写了一个函数,方便检查变量的类型,可以用来代替 typeof
[u]复制代码[/u] 代码如下:
function getType(o) { var _t; return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase(); }
执行结果: getType("abc"); //string getType(true); //boolean getType(123); //number getType([]); //array getType({}); //object getType(function(){}); //function getType(new Date); //date getType(new RegExp); //regexp getType(Math); //math getType(null); //null
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部