1.用typeof操作符判断基本数据类型和function类型
"undefined"——未定义
"boolean"——布尔
"string"——字符串
"number"——数值
"object"——对象或者null
"function"——函数
var param1 = false
var param2 = 'fdsafdsa'
var param3 = 123
var param4 = {}
var param5 = null
var param6 = function () {return 1}
var param7
console.log(typeof(param1),typeof(param2),typeof(param3))
console.log(typeof(param4),typeof(param5),typeof(param6),typeof(param7))
//boolean string number
//object object function undefined
此操作符可以判断除了object & null以外的所有类型(null是空对象指针)
2.判断是否为数组类型
主要就是数组对象和null不好判断
- instanceof
a instanceof Array检测Array的prototype是否在a的原型链上
var param1 = ['12', '23231', 123]
var param2 = { a: '123', b: '111' }
var param3 = null
var result = function (par) {
return (par instanceof Array)
}
console.log(result(param1)) //true
console.log(result(param2)) //false
console.log(result(param3)) //false
只有数组在这种情况下是true,是唯一,可以用于判断
- 原型链方法constructor
var param1 = ['12', '23231', 123]
var param2 = { a: '123', b: '111' }
var param3 = null
var param4
var result = function (par) {
if(par){
return (par.constructor == Array)
}else{
return 'result is null'
}
}
console.log(result(param1)) //true
console.log(result(param2)) //false
console.log(result(param3)) //result is null
console.log(result(param4)) //result is null
- Object.prototype.toString.call
var param1 = ['12', '23231', 123]
var param2 = { a: '123', b: '111' }
var param3 = null
var result = function (par) {
return Object.prototype.toString.call(par);
}
console.log(result(param1)) //[object Array]
console.log(result(param2)) //[object Object]
console.log(result(param3)) //[object Null]
Object.prototype.toString.call(o)=='[object Array]';用这个判断
- isArray
var param1 = ['12', '23231', 123]
var param2 = { a: '123', b: '111' }
var param3 = null
var result = function (par) {
return Array.isArray(par);
}
console.log(result(param1)) //true
console.log(result(param2)) //false
console.log(result(param3)) //false
3.判断是否为对象
- constructor
var param1 = ['12', '23231', 123]
var param2 = { a: '123', b: '111' }
var param3 = null
var param4 = function(){ return 1}
var param5 = new param4()
var result = function (par) {
if(par){
return par.constructor
}else{
return 'this is null'
}
}
console.log(result(param4)) //[Function: Function]
console.log(result(param1)) //[Function: Array]
console.log(result(param2)) //[Function: Object]
console.log(result(param3)) //this is null
console.log(result(param5)) //[Function: param4]
constructor可以用于判断array,object,null三种数据类型
- Object.prototype.toString.call()
最好用的方法,最推荐
var param1 = ['12', '23231', 123]
var param2 = { a: '123', b: '111' }
var param3 = null
var param4 = function(){ return 1}
var param5 = new param4()
var result = function (par) {
return Object.prototype.toString.call(par)
}
console.log(result(param1)) //[object Array]
console.log(result(param2)) //[object Object]
console.log(result(param3)) //[object Null]
console.log(result(param4)) //[object Function]
console.log(result(param5)) //[object Object]
- 比较constructor和Objcet.prototype.toString区别
function cla(){}
function cla2(){}
function cla3(){}
cla.prototype = new cla2();
var a = new cla();
var b = new cla3();
console.log(a.constructor); //[Function: cla2]
console.log(b.constructor) //[Function: cla3]
console.log(Object.prototype.toString.call(a)) //[object Object]
console.log(Object.prototype.toString.call(b)) //[object Object]
ps:constructor检测的是对象prototype上的类型,所以在new一个方法做对象的时候,检测出来的不会return objcet,会返回function方法名,而且可以修改par.prototype = new newClass()会被连带修改类型,导致检测结果不准确,所以不推荐使用constructor,更推荐万能的Objcet.prototype.toString.Call()
网友评论