2007-10-10 19:32 归类于:JavaScript
1、这两段为前文所讲的call和apply应用
JavaScript代码
- Function.prototype.bind = function (object){
- var __method = this;
- return function (){
- __method.apply(object,arguments);
- };
- };
- Function.prototype.bindAsEventListener = function (object){
- var __method = this;
- return function (event){
- __method.call(object,event||window.event);
- };
- };
2、指定时间后再执行该方法
JavaScript代码
- Function.prototype.timeOut = function(t) {
- var __method = this;
- var argu = Array.prototype.slice.call(arguments,1);
- var f = function(){
- __method.apply(null, argu);
- }
- return setTimeout(f, t);
- }
有别于系统的setTimeout,该方法可以向要执行的程序传递参数,下面是例子:
JavaScript代码
- function hello (arg1, arg2) {
- alert("arg1:" + arg1 + "\narg2:" + arg2);
- }
- hello.timeOut(1000, '第一个参数', '第二个参数');
该方法返回的值也可以用clearTimeout来清除



