Javascript 将调用函数分配给变量,然后调用它

Javascript 将调用函数分配给变量,然后调用它,javascript,Javascript,我正在努力弄清楚为什么以下方法不起作用: > var foo = Array.prototype.forEach.call; < undefined > typeof foo; < "function" > foo([1, 2, 3], function (y) {console.log(y);}); < Uncaught TypeError: foo is not a function at <anonymous>:2:1 a

我正在努力弄清楚为什么以下方法不起作用:

> var foo = Array.prototype.forEach.call;
< undefined

> typeof foo;
< "function"

> foo([1, 2, 3], function (y) {console.log(y);});
< Uncaught TypeError: foo is not a function
    at <anonymous>:2:1
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)

如有任何提示,我将不胜感激。

fn.call
始终需要上下文配合。您不能只将其分配给变量,因为您引用的是
函数.prototype.call
,而不包含任何上下文。请记住,
Function.prototype.call()
不是修饰符,而是帮助您处理正确上下文执行的函数属性。当您破坏引用时,您已经消除了它的用途

var foo = Array.prototype.forEach.call;
foo === Function.prototype.call //true
Array.prototype.forEach.call === Array.prototype.filter.call //true
Array.prototype.forEach.call === Object.prototype.hasOwnProperty.call //true
var foo = Array.prototype.forEach.call;
foo === Function.prototype.call //true
Array.prototype.forEach.call === Array.prototype.filter.call //true
Array.prototype.forEach.call === Object.prototype.hasOwnProperty.call //true