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

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));

 

posted @ 2015-04-14 21:27  徐大腿  阅读(228)  评论(0编辑  收藏  举报