四种方式
typeof、instanceof、构造函数、Object.prototype.toString.call()、jquery.type()
1. 类型
console.log(
typeof 100, //"number"
typeof 'abc', //"string"
typeof false, //"boolean"
typeof undefined, //"undefined"
typeof null, //"object"
typeof [1,2,3], //"object"
typeof {a:1,b:2,c:3}, //"object"
typeof function(){console.log('aaa');}, //"function"
typeof new Date(), //"object"
typeof /^[a-zA-Z]{5,20}$/, //"object"
typeof new Error() //"object"
typeof new Number(100), //'object'
typeof new String('abc'),// 'string'
typeof new Boolean(true),//'boolean'
);
基本数据类型中:Number、String、Boolean、undefined、参考数据类型中的Functionjquery 判断 类型,可以使用typeof来衡量数据类型,返回对应数据类型的大写字符。
另:使用typeof检查构造函数返回对象创建的Number、String、Boolean
在基本数据类型中:null。 参考数据类型包括:Array、Object、Date、RegExp。 不能用 typeof 检查。 将返回大写对象
2. 实例化
不仅用typeof判断,还用instanceof。 instanceof操作符需要指定一个构造函数,或者指定一个具体的类型,用于判断构造函数的原型是否在给定对象的原型链上。
console.log(
100 instanceof Number, //false
'dsfsf' instanceof String, //false
false instanceof Boolean, //false
undefined instanceof Object, //false
null instanceof Object, //false
[1,2,3] instanceof Array, //true
{a:1,b:2,c:3} instanceof Object, //true
function(){console.log('aaa');} instanceof Function, //true
new Date() instanceof Date, //true
/^[a-zA-Z]{5,20}$/ instanceof RegExp, //true
new Error() instanceof Error //true
)
其中基本数据类型有:Number、String、Boolean。 文字值不能用instanceof检查,而构造函数创建的值可以,如下:
var num = new Number(123);
var str = new String('dsfsf');
var boolean = new Boolean(false);
还需要注意的是,null 和 undefined 都会返回 false。 这是因为它们的类型是它们本身,并且它们不是由 Object 创建的,所以它们返回 false。
3.构造函数
constructor 是原型对象上的一个属性,指向构造函数。 根据实例对象查找属性的顺序,如果实例对象上没有实例属性或方法jquery 判断 类型,则会到原型链上查找。 因此,实例对象也可以使用构造函数属性。
如果输出类型实例的构造函数,它看起来像这样:
console.log(new Number(123).constructor)
//ƒ Number() { [native code] }
可以看出,它指向的是Number的构造函数,因此,可以使用num.constructor==Number来判断变量是否是Number类型。
var num = 123;
var str = 'abcdef';
var bool = true;
var arr = [1, 2, 3, 4];
var json = {name:'wenzi', age:25};
var func = function(){ console.log('this is function'); }
var und = undefined;
var nul = null;
var date = new Date();
var reg = /^[a-zA-Z]{5,20}$/;
var error= new Error();
function Person(){
}
var tom = new Person();
// undefined和null没有constructor属性
console.log(
tom.constructor==Person,
num.constructor==Number,
str.constructor==String,
bool.constructor==Boolean,
arr.constructor==Array,
json.constructor==Object,
func.constructor==Function,
date.constructor==Date,
reg.constructor==RegExp,
error.constructor==Error
);
//所有结果均为true
不仅是undefined和null,其他类型都可以通过constructor属性来确定。
4、使用Object.prototype.toString.call()测量对象类型
每个对象的类型可以通过toString()获得。 为了让Object.prototype.toString()测量到每个对象,需要通过Function.prototype.call()或Function.prototype.apply()来调用,将要检测的对象作为第一个参数传递,称之为Arg。
var toString = Object.prototype.toString;
toString.call(123); //"[object Number]"
toString.call('abcdef'); //"[object String]"
toString.call(true); //"[object Boolean]"
toString.call([1, 2, 3, 4]); //"[object Array]"
toString.call({name:'wenzi', age:25}); //"[object Object]"
toString.call(function(){ console.log('this is function'); }); //"[object Function]"
toString.call(undefined); //"[object Undefined]"
toString.call(null); //"[object Null]"
toString.call(new Date()); //"[object Date]"
toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
toString.call(new Error()); //"[object Error]"
由此可见,使用Object.prototype.toString.call()的形式来判断变量的类型是最准确的方式。
5、无敌万能的方法:jquery.type()
发表评论