简体   繁体   English

javascript箭头函数已传递未定义的参数,这如何工作?

[英]javascript arrow function has undefined argument passed, how does this work?

I am confused with the following snippet: 我对以下代码段感到困惑:

  let counted = countBy(text, char => {
      let script = characterScript(char.codePointAt(0));
      return script ? script.direction : "none";
  }).filter(({name}) => name != "none");

variable char gets defined inside the countBy function: I was trying to separate the arrow functions into separate functions so I can understand the code, but the variable char comes in the way. 在countBy函数内部定义了变量char:我试图将箭头函数分成单独的函数,以便可以理解代码,但是变量char妨碍了我。

I tried to separate the code so arrow functions were standalone functions, because I find the construct very confusing. 我尝试分离代码,使箭头函数成为独立函数,因为我发现该结构非常混乱。

// here are the 2 functions in full: I am new to JS :-( //这是完整的2个功能:我是JS的新手:-(

function dominantDirection(text) {
    let counted = countBy(text, char => {
        let script = characterScript(char.codePointAt(0));
        return script ? script.direction : "none";
  }).filter(({name}) => name != "none");

    if (counted.length == 0) return "ltr";

    return counted.reduce((a, b) => a.count > b.count ? a : b).name;
}
function countBy(items, groupName) {
      let counts = [];
      for (let item of items) {
          let name = groupName(item);
          let known = counts.findIndex(c => c.name == name);
          if (known == -1) {
              counts.push({name, count: 1});
          } else {
              counts[known].count++;
       }
     }
     return counts;
}

Your countBy function receives two things: 您的countBy函数收到两件事:

  1. An array of items 一系列items
  2. Another function that is called with every item in items , returning for each item its corresponding group name. 那个叫每另一个功能itemitems ,返回每个项目及其对应的组名。 The name of that other function within countBy is groupName , that's why it's invoked with groupName(item) . countBy中另一个函数的名称为groupName ,这就是为什么使用groupName(item)调用它的原因。

So your dominantDirection function receives a text and counts characters by direction, using an anonymous function that receives a character and does its logic with it (that's the function that countBy will call groupName ). 因此,您的dominantDirection函数使用匿名函数接收text并按方向对字符计数,该匿名函数接收字符并对其进行逻辑处理(这是countBy将调用groupName的函数)。 Maybe a more clear way of putting it would be: 也许更明确的说法是:

const getDirection = char => { // This line could also have been: function getDirection(char) {
  let script = characterScript(char.codePointAt(0));
  return script ? script.direction : "none";
}

function dominantDirection(text) {
    let counted = countBy(text, getDirection).filter(({name}) => name != "none");

    if (counted.length == 0) return "ltr";

    return counted.reduce((a, b) => a.count > b.count ? a : b).name;
}

I hope that helps clear out the confusion a bit, feel free to ask if it's still not clear! 我希望这有助于消除混乱,请随时询问是否仍然不清楚!

Btw if you want to read more about arrow functions, check the MDN web docs . 顺便说一句,如果您想了解更多有关箭头功能的信息,请查看MDN Web文档

You never answered my question about what ""dominantDirection()" is supposed to do. 您从未回答过有关““ dominantDirection()”应该做什么的问题。

I found the answer here: 我在这里找到了答案:

https://www.freecodecamp.org/forum/t/reading-eloquent-javascript-can-i-ever-get-this-good/181511 https://www.freecodecamp.org/forum/t/reading-eloquent-javascript-can-i-ever-get-this-good/181511

... a program to take a string and identify what percentage of whatever Unicode scripts are present in it out of the total. ...一个程序,用于获取字符串并确定其中所包含的所有Unicode脚本的百分比。 the program also accounts for Unicode characters which take up more than one code unit 该程序还考虑了占用多个代码单元的Unicode字符

Here's some very useful advice from the same thread: 这是来自同一线程的一些非常有用的建议:

That book ( Eloquent Javascript ) is not for everyone, including me :wink: and many others too don't like it… 那本书( Eloquent Javascript )并不适合所有人,包括我:wink:,还有许多其他人也不喜欢它……

A Better and Practical alternative for Eloquent Javascript is The Javascript Way followed by Programming for the Web with JavaScript . 口才Java的更好和实用的替代方法是“ Javascript Way”,然后是用JavaScript进行Web编程

Regarding your initial question, I hope lipusal's most excellent post gave you the answer you were looking for. 关于您的第一个问题,我希望lipusal的最出色帖子为您提供所需的答案。 In particular, these two snippets are basically equivalent: 特别是,这两个片段基本上是等效的:

// "Classic" JS syntax:
countBy(text, function(char) {
      var script = characterScript(char.codePointAt(0));
      return script ? script.direction : "none";
});

vs.

// ES6 syntax:
countBy(text, char => {
      let script = characterScript(char.codePointAt(0));
      return script ? script.direction : "none";
});

PS: PS:

From Amazon.com: 从Amazon.com:

Alright, but NOT a good book if you're just starting to learn JavaScript 好的,但是如果您刚刚开始学习JavaScript,那不是一本好书

and

not for beginners 不适合初学者

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

相关问题 作为参数传递的JavaScript typeof函数返回undefined - JavaScript typeof function passed as an argument returns undefined JavaScript 箭头函数语法。 BOX 作为过滤器是如何工作的? - JavaScript arrow function syntax. How does BOX as filter work? 如何确定一个函数的参数是否作为参数传递给另一个函数 - how to determine if the argument of a function passed into another function as an argument is undefined or not 从 Rails controller 传递给 Javascript function 的参数导致“未定义” - Argument passed to a Javascript function from a Rails controller results in “undefined” Object 作为参数传递给 function 时变为未定义,方法称为 JavaScript - Object turns into undefined when passed to function as argument and method is called JavaScript 当Javascript变量作为参数传递给函数时变得不确定 - Javascript variable becomes undefined when passed as an argument to a function 当作为 props 传递时,箭头函数未定义 - Arrow function is undefined when passed as props 为什么可以将参数传递给在 javascript 中没有参数的函数? - Why can argument(s) be passed to a function that has no parameter in javascript? Arrow函数如何在ECMAScript6中工作 - How does the Arrow function work in ECMAScript6 如何在JavaScript中的函数(本身就是一个参数)中传递参数 - How argument is being passed in the function(which itself is an argument) in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM