我看过 lodash filter文档并且不清楚第三个参数是否是上下文。
我正在使用 cytoscape 插件 (dagre),它似乎将 this
作为第 3 个参数传递。当我在调用过滤器方法之前暂停执行时,定义了 this
。但是在调用中 this
是未定义的。
我查看了 underscore filter文档,它似乎将第三个参数作为上下文。所以我有点猜测该插件最初使用下划线然后可能更改为 lodash。我正在从事的项目正在使用 lodash。
我当时无法理解为什么 this
为 null。它可能是特定于项目的,但我只想弄清楚 lodash 过滤器的第三个参数。
lodash的filter的定义和underscore的filter的定义是不是一模一样?从文档来看似乎并非如此。
最佳答案
好吧,您始终可以使用 Function.prototype.bind
定义自己的上下文。
_.filter([…],
function (o) {
console.log(this.id); //100
//than return something based on o
return o.active
}.bind({id: 100})
);
关于javascript - lodash 的过滤函数是否带上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47769206/