9
var shell = function (method) {
        window[method].apply(null, Array.prototype.slice.call(arguments, 1));
    };

shell('alert', 'monkey!');
shawndumas
  • 1,413
  • 15
  • 17

1 Answers1

10

alert.apply is the problem. Alert is not part of the language, so it may not be even a function. It is implementation-dependent.

gblazex
  • 49,155
  • 12
  • 98
  • 91
  • 4
    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 Jul 06 '10 at 12:48
  • 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 Jul 06 '10 at 12:56
  • 4
    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 Jul 06 '10 at 13:13
  • Stupid IE. I gave up and used eval if the better way fails. – shawndumas Jul 06 '10 at 14:13