您的位置:首页 > Web前端 > JavaScript

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

2015-04-14 21:27 393 查看
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));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: