2007-10-10 19:32 归类于:JavaScript

 

1、这两段为前文所讲的call和apply应用

JavaScript代码
  1. Function.prototype.bind = function (object){   
  2.     var __method = this;   
  3.     return function (){   
  4.         __method.apply(object,arguments);   
  5.     };   
  6. };   
  7.   
  8. Function.prototype.bindAsEventListener = function (object){   
  9.     var __method = this;   
  10.     return function (event){   
  11.         __method.call(object,event||window.event);   
  12.     };   
  13. };   

 

2、指定时间后再执行该方法

JavaScript代码
  1. Function.prototype.timeOut = function(t) {   
  2.     var __method = this;   
  3.   
  4.     var argu = Array.prototype.slice.call(arguments,1);   
  5.     var f = function(){   
  6.         __method.apply(null, argu);   
  7.     }   
  8.     return setTimeout(f, t);   
  9. }   

有别于系统的setTimeout,该方法可以向要执行的程序传递参数,下面是例子:

JavaScript代码
  1. function hello (arg1, arg2) {   
  2.     alert("arg1:" + arg1 + "\narg2:" + arg2);   
  3. }   
  4. hello.timeOut(1000, '第一个参数''第二个参数');  

该方法返回的值也可以用clearTimeout来清除

阅读:992 | 评论:0