Goodspeed

导航

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")) {}; //仅检查实例对象

 

posted on 2014-01-26 11:08  Goodspeed  阅读(428)  评论(0编辑  收藏  举报