//封装一个方法判断数组和对象
var o = {
'name':'lee'
};
var b ='22';
var a = ['reg','blue'];
function c(name,age){
this.name = name;
this.age = age;
}
var c = new c('kingw','27');
console.log(Object.prototype.toString.call(a));//[object Array]
console.log(Object.prototype.toString.call(o));//[Object Object]
console.log(Object.prototype.toString.call(c));//[Object Object]
console.log(Object.prototype.toString.call(b));//[object Array]
function isType(obj){
var type = Object.prototype.toString.call(obj);
if(type == '[object Array]'){
return 'Array';
}else if(type == '[object Object]'){
return "Object"
}else if(type == '[object Stringt]'){
return "string"
}else{
return 'param is no object type';
}
}
console.log(isType(o));//Object
console.log(isType(a));//Array
[object Array]
[object Object]
[object Object]
[object String]
Object
Array
undefined
console.log(isType(b));
param is no object type
undefined
希望大家看得懂!!!!!!!!!!!!!!!!!!!!