JS中的各种检测

 1 //null 只在肯定返回null值时才使用null比较
 2 var element = document.getElementById("my-div");
 3 if (element === null) {
 4 
 5 };
 6 //string number boolean undefined
 7 var num = 123;
 8 if (typeof num === "number") {
 9 
10 };
11 
12 /*
13 检查引用值
14 Date RegExp Error
15 跨域的检查会有问题
16 */
17 if (value instanceof Date) {
18 
19 };
20 
21 //检查函数
22 if (typeof myFunc === "function") {};
23 //if (myFunct instanceof Function) {}; 不能跨域
24 //浏览器函数 因为IE9之前返回有问题
25 if ("querySelectorAll" in document) {};
26 
27 //检查数组
28 function isArray(value){
29     if (typeof Array.isArray === function) {
30         return Array.isArray(value);
31     }else{
32         return Object.prototype.toString.call(value) === "[object Array]"; //IE9以下
33     }
34 }
35 
36 //检查属性
37 if ("related" in object) {};
38 if (object.hasOwnProperty("related")) {}; //仅检查实例对象

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值