When you call "apply" to a function, the function object itself is a "this" call to "apply", so you can do this:
function test(s) { alert(""+s); } Function.prototype.apply.call(setTimeout, null, [test, 0, 'abc']);
We basically force the use of Function.prototype.apply instead of looking for a nonexistent setTimeout.apply . In the call parameters, the first argument is the function that we want to run using apply (the context of the object, which in this case is setTimeout ). The following are the parameters that pass through apply , the first of which is the wait for this , the function will be run at ("null" in this case, since setTimeout does not allow the context of this value), and the next is an array of arguments to pass to the function that we want to run.
This works in IE7 +, except that IE7 does not pass user parameters (for example, "abc" in this example, which will request "undefined").
The TypeScript implementation is implemented here:
/** Helps support cases where 'apply' is missing for a host function object (ie IE7 'setTimeout', etc.). This function * will attempt to call '.apply()' on the specified function, and fall back to a work around if missing. * @param {Function} func The function to call '.apply()' on. * @param {Object} _this The calling object, which is the 'this' reference in the called function (the 'func' argument). * Note: This must be null for special host functions, such as 'setTimeout' in IE7. * @param {any} args The arguments to apply to given function reference (the 'func' argument). */ function apply(func: Function, _this: Object, args: any[]): any { if (func.apply) { return func.apply(_this, args); } else { return Function.prototype.apply.call(func, _this, args); } }
... and basic JavaScript:
function apply(func, _this, args) { if (func.apply) { return func.apply(_this, args); } else { return Function.prototype.apply.call(func, _this, args); } }