js类型检测

 1 <!DOCTYPE html>
 2 <html lang="zh">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta http-equiv="X-UA-Compatible" content="IE=edge">
 6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7     <title>Document</title>
 8 </head>
 9 <body>
10 
11 
12     <script>
13         function getType(val) {
14          return    Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()
15         }
16 
17         const isArray = (val) => Array.isArray(val)
18 
19         const isString = (val) => typeof val === 'string'
20 
21         const isNumber = (val) => typeof val === 'number'
22 
23         const isBoolean = (val) => typeof val === 'boolean'
24 
25         const isObject = (val) => getType(val) === 'object'
26 
27         const isNull = (val) => getType(val) === 'null'
28 
29         const uuid = (a) => a ? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16) : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid)
30 
31        // 产生随机数
32        const randomNumber = () => Math.floor(Math.random() * Date.now());
33 
34        // 判断该对象是否为空,
35     const isEmpty = (obj) => Object.keys(obj).length === 0 && obj.constructor === Object;
36     </script>
37 </body>
38 </html>

 

posted @ 2023-01-25 21:17  sky-su  阅读(22)  评论(0)    收藏  举报