简体   繁体   English

JavaScript返回函数的含义

[英]Javascript return function meaning

I understand return basically terminates a function. 我了解return基本上终止了一个功能。 But somehow in this case, I'm not sure which return is actually finishing up a function. 但是在这种情况下,我不确定哪个返回实际上完成了一个函数。

var THRESHOLD = 12;
var v = [5, 2, 16, 4, 3, 18, 20];
var res;

res = v.some(function(element, index, array) {
  console.log('element:', element);
  if (element >= THRESHOLD) {
    return true; //#1
  }

  return false; // #2
});
console.log('res:', res);

Say it's iterating at v[0]=5, it skips if(){} and go ahead to return false //2 , Why is that after #2 return, the function still keep looping? 假设它在v [0] = 5处进行迭代,它会跳过if(){}并继续return false //2 ,为什么在#2返回之后函数仍然保持循环?

The function does not keep running. 该功能无法继续运行。 It is being called several times because you are iterating over the array 它被多次调用,因为您正在数组上进行迭代

发生这种情况是因为第一个元素小于10,并且第一个返回false杀死了循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM