JavaScript array.every() 方法?

标签 javascript arrays

我正在查看 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 executing callback. [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/

相关文章:

javascript - 为什么数组显示为字符串,但当我将其打印到控制台时,它却将其打印为数组? ReactJS 功能组件

arrays - Node Mocha 数组应包含一个元素

javascript - 在 JavaScript 中创建嵌套对象值的 SUM

javascript - 为什么在React中未定义此数组?

javascript - 具有自引用的对象和数组

javascript - 如何解决这个sinopia安装错误?

javascript - 如何将正方形 div 对 Angular 分成 4 个可点击的部分?

javascript - 如何在 JavaScript 中定义一些东西

c - 我的 move 算法无法正常工作

javascript - 在 javascript 中手动执行事件