javascript - 在 Array.filter 中使用 Set.has - 不兼容的接收器

标签 javascript this

假设我有一个 Set 作为查找表。

const myset = new Set(["key1", "key2", "key3", "keepMe"]);

我想过滤 myset 中的其他一些键(例如 mykeys)的另一个数组。

const mykeys = ["ignoreMe", "keepMe", "ignoreMeToo", "key2"];

问题:

为什么我必须使用

const filtered = mykeys.filter(k => myset.has(k))

而不是

const filtered = mykeys.filter(myset.has)
// TypeError: Method Set.prototype.has called on incompatible receiver undefined

也就是说,为什么我必须在过滤器中创建匿名 lambda 函数? keys.has 具有相同的签名(参数 - 元素,返回 bool 值)。一位 friend 告诉我这与this有关。

mykeys.map(console.log) 工作时没有错误(尽管没有多大用处)。

我遇到了这个article at MDN我仍然不明白为什么“'myset'没有被捕获为这样”。我理解解决方法,但不知道“为什么”。任何人都可以用一些细节和引用以人性化的方式解释它吗?

更新:感谢大家的回复。也许我并不清楚我在问什么。我确实了解解决方法。

@charlietfl 明白了。这是his comment ,我正在寻找的东西:

Because filter() has no implicit this where as set.has needs to have proper this context. Calling it inside anonymous function and manually adding argument makes the call self contained.

最佳答案

您可以使用Array#filterthisArg使用 set 和 has 的原型(prototype)作为回调。

此模式不需要绑定(bind) Set 的实例到原型(prototype),因为

If a thisArg parameter is provided to filter, it will be used as the callback's this value. Otherwise, the value undefined will be used as its this value. The this value ultimately observable by callback is determined according to the usual rules for determining the this seen by a function.

const
    myset = new Set(["key1", "key2", "key3", "keepMe"]),
    mykeys = ["ignoreMe", "keepMe", "ignoreMeToo", "key2"],
    filtered = mykeys.filter(Set.prototype.has, myset);

console.log(filtered);

关于javascript - 在 Array.filter 中使用 Set.has - 不兼容的接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50544029/

相关文章:

javascript - 一起使用 beforeunload 和 unload 吗?

javascript - CKEditor 插件按钮在源模式下禁用

c++ - 宏和函数中的“this”指针

java - Java中的Class.this和this有什么区别

c++ - 在模板派生类中,为什么我需要在成员函数内使用 "this->"限定基类成员名称?

javascript - mocha测试中以下js箭头函数中 `this`指向什么?

时间:2019-03-17 标签:c#app: Is it possible to implement a JSON interface?

用于插入新 pre 标记的 JavaScript 事件处理程序

javascript - typescript 错误? `this` `get` 语法和 `defineProperty` 的 `get` 之间的差异

javascript - 定时图像变化