为什么我需要在node.js中编写“function(value){return my_function(value);}”作为回调?

时间:2013-12-14 02:27:49

标签: javascript node.js callback

对JS来说是全新的,所以请原谅这是否令人难以置信。

假设我想要使用函数f过滤字符串列表,该函数f映射字符串 - >布尔。这有效:

filteredList = list.filter(function(x) { return f(x); })

这失败了:

filteredList = list.filter(f)

为什么???

代码示例:

 ~/projects/node (master)$ node
> var items = ["node.js", "file.txt"]
undefined
> var regex = new RegExp('\\.js$')
undefined
> items.filter(regex.test)
TypeError: Method RegExp.prototype.test called on incompatible receiver undefined
    at test (native)
    at Array.filter (native)
    at repl:1:8
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
> items.filter(function(value) { return regex.test(value); } )
[ 'node.js' ]
> 

3 个答案:

答案 0 :(得分:18)

您正在传递对“test”函数的引用,但是当它被调用时,正则表达式对象将不会出现。换句话说,在“test”中,this的值将为undefined

你可以避免这种情况:

items.filter(regex.test.bind(regex))

.bind()方法将返回一个函数,该函数始终以“正则表达式”的值运行为this

答案 1 :(得分:6)

您经常无法做到这一点的原因是用作方法的函数不仅仅是方法。如果您在不调用它们的情况下使用它们,那么它们就会脱离原始上下文。您可以使用Function.prototype.bind

解决此问题
items.filter(regex.test.bind(regex));

答案 2 :(得分:1)

正如其他人所说,调用函数引用时,thisundefined。我想提供一个非反身的,稍微冗长的替代方法:

items.filter(RegExp.prototype.test.bind(/regex/g));