我在写setTimeout函数的时候遇到了这样一个问题:setTimeout(fn, 3000),我希望函数fn在执行的期间可以指定一个当前的this对象。解决的方法如下
Function.prototype.getBindToFn = function(currentThis) { var t = this; return function() { return t.apply(currentThis, arguments); }; }
所以代码可以改写为
setTimeout(fn.getBindToFn(currentThis), 3000)
问题解决。