js code is very simple, the key is to understand some essential things in it, here is just some code for reference only;
(function(root,factory){
console.log(root);
console.log(factory)
if(typeof module === 'undefined'){
root.$ = root.DN = factory();
}else if( typeof module === 'object' || typeof module.export === 'object'){
module.export = factory();
}
})(this, function(){
var DN = {
toType:function(obj){
if(obj == null ){
return obj+'';
}
return typeof obj === "object" || typeof obj === "function" ?class2type[Object.prototype.toString.call(obj)] || 'object' : typeof obj;
}
};
var class2type = {};
"Boolean Number String Function Array Date RegExp Object Error Null Undefined".split(' ').map(function(item, index) {
class2type["[object " + item + "]"] = item.toLowerCase();
});
return DN;
})
The above code is placed in the 1.js file, and the file is directly referenced in HTML, then the toType() method can be used, which is simple and convenient:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>判断类型</title>
</head>
<body>
<!-- 架构
1、如果是基本类型,就使用typeof;
2、如果是引用类型就使用 toString;
3、统一类型的大小写; -->
<script type="text/javascript" src="./1.js"></script>
<script type="text/javascript">
console.log($.toType({}));
</script>
</body>
</html>