JavaScript 检测对象类型

function type(o){
                var t, c, n;
                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())){
                    alert('s')
                    return n;
                }
                return 'Object';
            }
            function classof(o){
                return Object.prototype.toString.call(o).slice(8,-1);
            }
            Function.prototype.getName = function(){
                if('name' in this) return this.name;
                console.log(('name' in this));
                return this.name = this.toString().match(/function\s*([^(]*)\(/)[1];
            }
            function Tool(){

            }
            var t = new Tool();
            var m = function(){

            }
            console.log(m.constructor.name);

 

posted @ 2013-08-20 08:26  fanhc019  阅读(197)  评论(0编辑  收藏  举报