var shell = function (method) {
window[method].apply(null, Array.prototype.slice.call(arguments, 1));
};
shell('alert', 'monkey!');
views:
142answers:
1
+10
A:
alert.apply is the problem. Alert is not part of the language, so it may not be even a function. It is implementation-dependent.
galambalazs
2010-07-06 12:46:00
Right - IE exposes APIs on `window` and on DOM elements to Javascript, but the exposure is limited and you generally cannot treat such things as if they were "real" Javascript components.
Pointy
2010-07-06 12:48:04
+1 agree with galambalazs.
Krunal
2010-07-06 12:49:49
Good point, but then shouldn't this work: var shell = function (method) { var fn = window[method]; fn.apply = Function.prototype.apply; fn.apply(null, Array.prototype.slice.call(arguments, 1)); }; shell('alert', 'monkey!');
shawndumas
2010-07-06 12:56:22
No. The problem is with the fact that `window` is a host object and `alert` is a property of a host object, and host objects are not obliged to behave like native objects. The following question is similar to yours: http://stackoverflow.com/questions/3060318/js-proxy-pattern-problem
Tim Down
2010-07-06 13:13:24
Stupid IE. I gave up and used eval if the better way fails.
shawndumas
2010-07-06 14:13:34