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

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

因为IE是邪恶的 – Jimmy 2010-07-06 12:40:12

+1

请参阅此问题的答案:http://stackoverflow.com/questions/120804/difference-between-array-slice-and-array-slice – 2010-07-06 12:45:20

+1

no,Array.prototype.slice.call (参数,1)没问题。 – galambalazs 2010-07-06 12:46:24

回答

10

alert.apply是问题所在。 警报不是语言的一部分,所以它可能不是一个函数。它是执行相关的

+4

Right - IE在'window'和DOM元素上向Javascript公开API,但是暴露程度有限,并且您通常不能将这些事情看作是“真正的”Javascript组件。 – Pointy 2010-07-06 12:48:04

+0

+1与galambalazs一致。 – Krunal 2010-07-06 12:49:49

+0

好点,但不应该这样工作: 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

相关问题