简体   繁体   English

没有操作员的“ if”如何工作?

[英]How does this “if” without operators work?

I am relatively new to Javascript and am working through ch. 我对Java相对较新,并且正在学习ch。 5 of Eloquent Javascript. 五种口才的Javascript。 I came across some code that I don't quite understand. 我遇到了一些我不太理解的代码。 I know HOW it works (the general method and steps), but I don't understand WHY it works. 我知道它是如何工作的(一般方法和步骤),但是我不明白为什么它会工作。

The code is here: 代码在这里:

function filter(array, test) {
  var passed = [];
  for (var i = 0; i < array.length; i++) {
    if (test(array[i]))
      passed.push(array[i]);
  }
  return passed;
}

Basically the function takes the element the 'for loop is iterating over' from the array, and compares it to the test parameter. 基本上,该函数从数组中获取“ for循环正在迭代”的元素,并将其与测试参数进行比较。

I am wondering how/why this works: 我想知道这如何/为什么起作用:

if (test(array[i]))

There is no || 没有|| && or other 'comparison operators'. &&或其他“比较运算符”。 How does it compare values with only using parenthesis? 如何仅使用括号将值进行比较?

How is test compared to the array[i] value with no operators? 如何将测试与没有运算符的array [i]值进行比较?

Link to file: http://eloquentjavascript.net/05_higher_order.html go to 'Filtering an Array' exercise 链接到文件: http : //eloquentjavascript.net/05_higher_order.html转到“过滤数组”练习

Thanks! 谢谢!

Whatever is inside the parentheses of an if statement will be evaluated. if语句括号内的内容将被评估。 If the result is falsy ( false , 0 , "" , NaN , null , undefined ) it fails and if the result is truthy (everything else) it passes. 如果结果为假( false0""NaNnullundefined ),它将失败;如果结果为真(其他所有条件),则通过。 So if your if statement contains a function call if (test(something)) {} , then the if statement just says if the result of the function call is truthy then pass . 因此,如果您的if语句包含一个if (test(something)) {}函数调用,则if语句仅说明if the result of the function call is truthy then pass

Also, && and || 此外, &&|| are not comparison operators, they are boolean operators. 不是比较运算符,而是布尔运算符。 They just connect the result of two statements, like true || false 它们只是连接两个语句的结果,如true || false true || false which evaluates to true . true || false ,其值为true

I am not quite sure, but I think this is a custom function. 我不太确定,但是我认为这是一个自定义函数。 Most likely there is some comparison there and the result of the function is True/False. 最有可能存在一些比较,该函数的结果为True / False。 If you give us the whole code we could explain it better to you. 如果您将整个代码提供给我们,我们可以为您更好地解释。

This code is accepting a test parameter that is what is called a "predicate function" ie a function that given an element will return true or false. 该代码接受一个称为“谓词函数”的test参数,即给定元素的函数将返回true或false。

It's going to be used for example with 例如,它将与

var big_numbers = filter(numbers, function(x){ return x > 100; });

ie the expected parameter test is actually code . 即预期的参数测试实际上是代码

In Javascript passing code is very common and idiomatic. 在Javascript中,传递代码非常普遍且惯用。 It's something that is more annoying in other languages that don't support the concept of "closure" and of "nested function", forcing all code to live at the top level, being given a name and to have no context (eg the C language). 在不支持“闭包”和“嵌套功能”概念的其他语言中,这更令人讨厌,这迫使所有代码都位于顶层,被赋予名称且没有上下文(例如,C语言)语言)。

'test' here is a function, not a value. 这里的“测试”是一个函数,而不是一个值。 In Javascript, each function is an object and can be passed as parameter. 在Javascript中,每个函数都是一个对象,可以作为参数传递。 In this case, test is a function that take one parameter and return true or false base on the parameter value. 在这种情况下,test是一个具有一个参数并根据参数值返回true或false的函数。

So in the for loop, test function is called with each array element and if the result is true, it will be store in another array. 因此,在for循环中,将对每个数组元素调用test函数,如果结果为true,它将存储在另一个数组中。 Eventually, passed elements would be return to the function caller. 最终,传递的元素将返回给函数调用者。

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

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