一、javascript:typeof 返回的是字符串,有六种可能, "number"、"string"、"boolean"、"object"、"function"、"undefined"

  • 是否为字符串:"", "false", "0", "Hello World"
var isString = function(obj){
	return typeof obj === 'string';
}
var isString = function(obj){
    return Object.prototype.toString.call(obj) === '[object String]';
};
  • 是否为布尔值:false, true
var isBoolean = function(obj){
	return typeof obj === 'boolean';
}
var isBoolean = function(obj){
    return Object.prototype.toString.call(obj) === '[object Boolean]';
};
  • 是否为数值:0, 3.14, -1, NaN
var isNumber = function(obj){
	return typeof obj === 'number';
}
var isNumber = function(obj){
    return Object.prototype.toString.call(obj) === '[object Number]';
};
  • 是否为undefined值
var isUndefined = function(obj){
    return typeof obj === 'undefined';
}
var isUndefined = function(obj){
    return Object.prototype.toString.call(obj) === '[object Undefined]';
};
  • 是否为null值:( typeof null === 'object' )
var isNull = function(obj){
    return Object.prototype.toString.call(obj) === '[object Null]';
};
  • 是否为数组:( typeof [] === 'object' )
var isArray = function(obj){
    if(Array.isArray){
        return Array.isArray(obj);
    }
    return Object.prototype.toString.call(obj) === '[object Array]';
}
  • 是否为方法/函数
var isFunction = function(obj){
    return typeof obj === 'function';
}
var isFunction = function(obj){
    return Object.prototype.toString.call(obj) === '[object Function]';
}