javascript常用的小方法

1、求阶乘

1 function factorial(num){
2     if(num<=1){
3         return 1;
4     }else{
5         return num*arguments.callee(num-1);
6     }
7 }

2、求随机数n到m之间的数,包含m和n

1 function selectFrom(lowerValue,upperValue){
2     return Math.floor(Math.random()*(upperValue-lowerValue+1)+lowerValue);
3 }

3、判断属性是存在与对象还是存在于原型中,True为原型中,False为对象中

1 function hasPrototypeProperty(object,name){
2     return !object.hasOwnProperty(name)&&(name in object);
3 }

4、用于检测是否为数组

1 function isArry(value){
2     return Object.prototype.toString.call(value)=="[object Array]";
3 }

5、用于检测是否为函数

1 function isFunction(value){
2     return Object.prototype.toString.call(value)=="[object Function]";
3 }

6、用于检测是否为正则表达式

1 function isRegExp(value){
2     return Object.prototype.toSource.call(value)=="[object RegExp]";
3 }

7、使用下面代码可以跨浏览器取得窗口左边和上边的位置

1 var leftPos=(typeof window.screenLeft=="number")?window.screenLeft:window.scrollX;
2 var topPos=(typeof window.screenTop=="number")?window.screenTop:window.scrollY;

8、获取页面视口的大小

 1 var pageWidth=window.innerWidth,pageHeight=window.innerHeight;
 2 if(typeof pageWidth!="number"){
 3     if(document.compatMode=="CSS1Compat"){//判断是否属于标准模式
 4         pageWidth=document.documentElement.clientWidth;
 5         pageHeight=document.documentElement.clientHeight;
 6     }else{
 7         pageWidth=document.body.clientWidth;
 8         pageHeight=document.body.clientHeight;
 9     }
10 }

 9、获取href参数数组

 1 function getQueryStringArgs(){
 2     //获取查询字符串并去掉开头的问号
 3     var qs=(location.search.length > 0 ? location.search.substring(1) : ""),
 4         //保存数据的对象
 5         args={},
 6         //取得每一项
 7         items=qs.length ? qs.split("&"):[],
 8         item=null,
 9         name=null,
10         value=null,
11         //在for循环中使用
12         i= 0,
13         len=items.length;
14     for(i=0;i<len;i++){
15         item=items[i].split("=");
16         name=decodeURIComponent(item[0]);
17         value=decodeURIComponent(item[1]);
18         if(name.length){
19             args[name]=value;
20         }
21     }
22     return args;
23 }

 10、将原型对象作为通用函数的参数,让通用函数返回创建的对象(每次创建完对象退出New函数作用域时,临时的new_函数对象会被自动释放。由于new_的prototype属性被设置为新的原型对象,其原来的原型对象和new_之间就已解开了引用链,临时函数及其原来的原型对象都会被正确回收了。)

 1 function New(aClass,aParams){//通用创建函数
 2     function new_(){ //定义临时的中转函数壳
 3         aClass.Create.apply(this,aParams); //调用原型中定义的构造函数,中转构造逻辑及构造函数
 4     };
 5     new_.prototype=aClass; //准备中转原型对象
 6     return new new_();  //返回建立最终建立的对象
 7 }
 8 var Person={ //定义的类
 9     Create:function(name,age){
10         this.name=name;
11         this.age=age;
12     },
13     SayHello:function(){
14         alert("Hello,I'm "+this.name);
15     },
16     HowOld:function(){
17         alert(this.name+" is "+this.age+" years old.");
18     }
19 };
20 var BillGates=New(Person,["Bill Gates",53]); //调用通用函数创建对象,并以数组形式传递构造函数参数
21 BillGates.SayHello();
22 BillGates.HowOld();

 3、添加和移除处理程序

 

 1 var EventUtil={
 2     addHandler:function(element,type,handler){
 3         if(element.addEventListener){
 4             element.addEventListener(type,handler,false);
 5         }else if(element.attachEvent){
 6             element.attachEvent("on"+type,handler);
 7         }else{
 8             element["on"+type]=handler;
 9         }
10     },
11     getEvent:function(event){
12         return event?event:window.event;
13     },
14     getTarget:function(event){
15         return event.target||event.srcElement;
16     },
17     preventDefault:function(event){
18       if(event.preventDefault){
19           event.preventDefault();
20       }else{
21           event.returnValue=false;
22       }
23     },
24     removeHandler:function(element,type,handler){
25         if(element.removeEventListener){
26             element.removeEventListener(type,handler,false);
27         }else if(element.detachEvent){
28             element.detachEvent("on"+type,handler);
29         }else{
30             element["on"+type]=null;
31         }
32     },
33     stopPropagation:function(event){
34         if(event.stopPropagation){
35             event.stopPropagation();
36         }else{
37             event.cancelBubble=true;
38         }
39     }
40 };

 

posted @ 2012-11-27 14:58  xu_happy_you  阅读(477)  评论(0编辑  收藏  举报