メソッドをに渡すとsetTimeout()
、グローバルスコープで実行されます。 実行時this
にポイントします。window
詳しくはこちらをご覧ください。
グローバルでない場合foo
、それは見つかりません、ergo ReferenceError
。
var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval;
// just backed up the defaults. Now basically creating timeout and setInterval
//functions that take scope as a parameter,
//so you can use them in whichever invocation context you want.
window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
return __nativeST__(vCallback instanceof Function ? function () {
vCallback.apply(oThis, aArgs);
} : vCallback, nDelay);
};
window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
return __nativeSI__(vCallback instanceof Function ? function () {
vCallback.apply(oThis, aArgs);
} : vCallback, nDelay);
};