views:

142

answers:

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

shell('alert', 'monkey!');
+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
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
+1 agree with galambalazs.
Krunal
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
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
Stupid IE. I gave up and used eval if the better way fails.
shawndumas