这种方法如何使用从未给过值的参数

这种方法如何使用从未给过值的参数

本文介绍了这种方法如何使用从未给过值的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天遇到了 .every() 方法并找到了这个示例代码来解释它是如何工作的:

I came across the .every() method today and found this example code to explain how it works:

var ages = [32, 33, 16, 40];

function checkAdult(age) {
    return age >= 18;
}

function myFunction() {
    document.getElementById("demo").innerHTML = ages.every(checkAdult);
}

myFunction() 的结果是 false,我明白这个方法在做什么(遍历数组的每个值并检查该值是否为 >= 18,但我不明白 .every() 方法如何能够在其返回语句中使用 age 参数而无需 age 在方法调用中声明.

The result of myFunction() is false, and I understand what the method is doing (going through each value of the array and checking if the value is >= 18, but I don't understand how the .every() method is able to use the age parameter in its return statement without age being declared in the method call.

该方法是否会自动知道它应该引用它正在查看的数组的索引?这是我能想到的唯一解释,但我在网上找不到任何解释.

Does the method somehow automatically know that it should be referencing the index of the array it's looking at? That's the only explanation I can come up with, but I can't find any explanation of this online.

推荐答案

在方法调用中声明的.您根本没有关注方法调用.

It is declared in the method call. You simply aren't looking at the method call.

checkAdult 作为参数传递给 every.every 中的代码调用该函数并向其传递一个值.

checkAdult is passed as an argument to every. The code inside every calls that function and passes it a value.

这篇关于这种方法如何使用从未给过值的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:55