我正在查看 Javascript .every
文档,它有第二个参数,该参数传递给描述
Optional. Value to use as this when executing the callback. If a thisArg parameter is provided to every, it will be used as callback's this value. Otherwise, the value undefined will be used as its this value. The this value ultimately observable by the callback is determined according to the usual rules for determining the this seen by a function.
这意味着我可以在回调函数中以 this
的形式访问该值,这方面的实际示例是什么?
最佳答案
根据 MDN,
Optional. Value to use as
this
when executingcallback
. [Ref]
这意味着,您可以在回调函数中使用提供的值作为 this
来测试该值。
const data = [0, 1, 2, 3];
const doesPass = data.every(function(el) {
return el < this.toTest;
}, {
toTest: 4
});
console.log(doesPass);
关于JavaScript array.every() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50084952/