var shell = function (method) {
window[method].apply(null, Array.prototype.slice.call(arguments, 1));
};
shell('alert', 'monkey!');
Asked
Active
Viewed 355 times
9

shawndumas
- 1,413
- 15
- 17
-
12because IE is evil – Jimmy Jul 06 '10 at 12:40
-
1See the answer to this question: http://stackoverflow.com/questions/120804/difference-between-array-slice-and-array-slice – Tim S. Van Haren Jul 06 '10 at 12:45
-
1no, Array.prototype.slice.call(arguments, 1) is ok. – gblazex Jul 06 '10 at 12:46
-
1@Tim he's already using `Array.prototype.slice` – Pointy Jul 06 '10 at 12:46
-
@Jimmy this is not the case here. The standard doesn't insist this behavior. – gblazex Jul 06 '10 at 12:53
1 Answers
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
-
4Right - 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
-
4No. 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
-