码迷,mamicode.com
首页 > 编程语言 > 详细

javascript——可以判断值的类型的函数

时间:2015-04-14 22:52:12      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

function classof(o){
    return Object.prototype.toString.call(0).slice(8,-1);
}
Function.prototype.getName= function () {
  return this.name ||this.toString().match(/function\s*([^()*]\(/)[1];
};
function type(o){
    var t, c,n;//type class name
    if(o===null) return "null";

    if(o!==o) return "nan";

    if((t=typeof o)!==‘object‘) return t;

    if((c=classof(o) )!==‘object‘) return c;

    if(o.constructor && typeof o.constructor === ‘function‘ && (n= o.constructor.getName())) return n;
}




console.log(type({}));
console.log(type(""));
console.log(type([1,2,3]));
console.log(type(NaN));
console.log(type(undefined));
console.log(type(0));
console.log(type(null));
console.log(type(Array));
console.log(type(false));

 

javascript——可以判断值的类型的函数

标签:

原文地址:http://www.cnblogs.com/goesby/p/4426148.html

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