Quick actions

cmd+k|ctrl+k

Navigation

Languages

js判断类型 

Snippet info

Language

JavaScript

Visibility

public

Author

pheker

Created

2017-07-19T06:55:33Z

Updated

2017-07-19T06:55:33Z



/**
 *  判断类型 
 */
function typeOf(o){
    var type = Object.prototype.toString.call(o);
    var name = type.substring(1,type.length-1).split(" ")[1].toLowerCase();
    return {
        type:type,
        name:name,
        isNull:function(){
            return this.name==="null";
        },
        isUndefined:function(){
            return this.name==="undefined";
        },
        isNumber:function(){
            return this.name==="number";
        },
        isBoolean:function(){
            return this.name==="boolean";
        },
        isString:function(){
            return this.name==="string";
        },
        isObject:function(){
            return this.name==="object";
        },
        isArray:function(){
            return this.name==="array";
        },
        isFunction:function(){
            return this.name==="function";
        }
    };
}


var func = function fn(){}
var types = [null,undefined,1,true,"Pheker",{name:"zhang"},[1],func];
types.map(function(v,i){
    console.log(typeOf(v).name);
})

console.log(typeOf(types[0]).isNull())








INFO