js 判断数据类型

 

以前都是基于框架写东西,当开始自己开始基于底层开发一些东西的时候才发现,当初不当回事的数据类型在一些高级程序中有多么的重要!

整理,总结,积累成长

废话少说:

function typeOf(obj){
if(typeof obj ==='object' || typeof obj ==='function'){
if(typeof(obj) === "object"){
if(typeof(obj) === "object" && Object.prototype.toString.call(obj).toLowerCase() === "[object object]" && !obj.length){
return "Json";
}
if(obj && typeof obj==='object' && typeof obj.length==='number' && typeof obj.splice==='function' && !(obj.propertyIsEnumerable('length'))){
return "Array";
}
if(typeof(obj) === "object" && obj !== undefined){
return "Null";
}
if(typeof(obj) === "object" && obj.toString()==="[object HTMLDivElement]" && obj.nodeName){
//return obj.nodeName;
return "Node";
}
if(typeof(obj) === "object" && obj.toString()==="[object NodeList]"){
return "NodeList";
}
if(typeof(obj) === "object" && obj.toString()==="[object Window]"){
return "Window";
}
};

if(typeof obj ==='function'){
return "Function";
}
}else{
return typeof obj;
}
}

 

posted on 2016-07-18 20:13  songyijian321  阅读(170)  评论(0编辑  收藏  举报

导航