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

function classof(o){
    return Object.prototype.toString.call(0).slice(8,-1);
}
Function.prototype.getName= function () {
  return this.name ||this.toString().match(/functions*([^()*](/)[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));
原文地址:https://www.cnblogs.com/goesby/p/4426148.html