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

shell('alert', 'monkey!'); 
+12

weil IE ist böse – Jimmy

+1

Siehe die Antwort auf diese Frage: http://stackoverflow.com/questions/120804/difference-between-array-slice-and-array-slice –

+1

nein, Array.prototype.slice.call (Argumente, 1) ist in Ordnung. – galambalazs

Antwort

10

alert.apply ist das Problem. Alert ist kein Teil der Sprache, es kann also nicht einmal eine Funktion sein. Es ist implementierungsabhängig.

+4

Richtig - IE exponiert APIs auf 'window' und auf DOM-Elementen auf Javascript, aber die Exposition ist begrenzt und Sie können solche Dinge im Allgemeinen nicht behandeln, als wären sie" echte "Javascript-Komponenten. – Pointy

+0

+1 zustimmen mit Galambalazs. – Krunal

+0

Guter Punkt, aber dann sollte das nicht funktionieren: var shell = Funktion (Methode) { var fn = Fenster [Methode]; fn.apply = Function.prototype.apply; fn.apply (null, Array.prototype.slice.call (Argumente, 1)); }; Shell ('Alarm', 'Affe!'); – shawndumas