简体   繁体   English

以下代码是什么意思?我应该如何理解函数体的语法?

[英]What does the following code mean??And how should I understand the syntax of the function body?

createFilter(queryString) {
  return (restaurant) => {
    return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
  };
}

I'm wondering that why the first return is followed by another return , and what dose the symbol => mean? 我想知道为什么第一个return之后是另一个return ,并且符号=>意味着什么? How should I comprehend the whole piece of code? 我应该如何理解整个代码?

The => symbol is just another way of declaring a function. =>符号只是声明函数的另一种方式。 This type of function is known as an arrow function (or sometimes referred to as a lambda function). 这种类型的功能称为箭头功能 (或有时称为lambda函数)。 For example, (restaurant) => {...} can be re-written as: 例如, (restaurant) => {...}可以重写为:

function(restaurant) {...}

While this isn't exactly the same as the arrow function, it will help you understand what's going on in your code. 虽然它与箭头功能不完全相同,但是它将帮助您了解代码中正在发生的事情。 To understand the core difference between arrow functions and plain function syntax, you can read this answer . 要了解箭头函数和普通函数语法之间的核心区别,您可以阅读此答案

As for the code logic, if you look at the inner-function by itself it may become clearer what's happening: 至于代码逻辑,如果您单独查看内部函数,可能会更清楚地了解发生了什么:

(restaurant) => {
  return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
};

This is a function, which accepts a restaurant as an argument. 这是一个接受restaurant作为参数的函数。 The function then uses the restaurant to return a value once called . 然后,该函数使用restaurant return 一次调用的值。 In this case, the return value is a boolean. 在这种情况下,返回值是布尔值。

If you were to call the entire function above x , then your entire code would look something like: 如果要在x上方调用整个函数,则整个代码应类似于:

createFilter(queryString) {
  return x;
}

Here, it is clear that that the function createFilter will accept an argument, queryString , and return x which we know is a function. 在这里,很明显函数createFilter将接受参数queryString ,并返回x ,我们知道这是一个函数。

So, if I was to call createFilter("foo") , it would give me the function x , which we said is equivalent to: 因此,如果我要调用createFilter("foo") ,它将给我函数x ,我们说的等效于:

(restaurant) => {
  return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
};

So, now, as we know x is a function, we can call it x("bar") . 因此,现在,我们知道x是一个函数,我们可以将其称为x("bar") We can see that the above function will return a boolean (as it is doing a comparison), and so calling x("bar") will result in either true or false . 我们可以看到上面的函数将返回一个布尔值(因为它正在进行比较),因此调用x("bar")将导致truefalse

Full usage of the createFilter function will thus looking something like: 因此,充分利用createFilter函数将类似于:

let filter = createFilter("foo"); // returns a function (x), we can store the returned function in a variable called "filter"
let found = filter("bar"); // call the function stored in the variable filter

Or, by removing the itermidiate variables, it can be written as one-line: 或者,通过删除itermidiate变量,可以将其写为一行:

let found = createFilter("foo")("bar");
// returns a function--^^^        ^^---- executes the returned function    

(restaurant) => { } is a function. (restaurant) => { }是一个函数。 More specifically it is an arrow function . 更具体地说,它是箭头功能

Welcome to SO. 欢迎来到SO。 Sounds like you have a lot of JS reading to do. 听起来您有很多JS阅读要做。 What the code is doing: createFilter returns a function ( => is an "arrow function") which when called, returns the true or false based on if the comparison holds. 代码的作用: createFilter返回一个函数( =>是“箭头函数”),该函数在调用时根据比较是否成立返回truefalse The comparison is checking if queryString.toLowerCase() matches the first letter ( indexOf where the index is 0 ) of restaurant.value.toLowerCase() . 比较是检查queryString.toLowerCase()匹配restaurant.value.toLowerCase()的第一个字母(索引为0 indexOf restaurant.value.toLowerCase()

So for example, you could call it like: 因此,例如,您可以这样称呼它:

const myFilter = createFilter('A');
myFilter('Algerian Food'); // true
myFilter('Italian Food'); // false

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

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