简体   繁体   English

将胖箭头功能转换为普通功能

[英]Converting fat arrow function to a normal function

I am curious to know why this 我很想知道为什么会这样

let updateCatInfo = e.sender.dataSource._data.find(x => x.CatalogID == catID );

can't be converted to this 无法转换为此

let updateCatInfo = e.sender.dataSource._data.find(function (x) { x.CatalogID == catID });

The reason why I am asking is because I am using .NET Web Browser Control and for some reason its not liking the fat arrow function (as seen in the first snippet) and tried to convert it to the second snippet. 我问的原因是因为我使用的是.NET Web浏览器控件,并且由于某种原因它不喜欢胖箭头功能(如第一个片段中所示)并试图将其转换为第二个片段。 But the second snippet doesn't work. 但第二个片段不起作用。

So I am left to do this 所以我不得不这样做

for (let i = 0; i < e.sender.dataSource._data.length; i++) {
    if (e.sender.dataSource._data[i].CatalogID == catID) {
        updateCatInfo = e.sender.dataSource._data[i];
        break;
    }
}

for all the find functions that use a fat arrow function 对于使用胖箭头函数的所有查找函数

According to the docs regarding Arrow Functions > Function bod y: 根据有关箭头功能>功能的文档:

Function body 功能体

Arrow functions can have either a "concise body" or the usual "block body". 箭头功能可以具有“简洁的身体”或通常的“块体”。

In a concise body, only an expression is specified, which becomes the implicit return value. 在简洁的主体中,只指定了一个表达式,该表达式成为隐式返回值。 In a block body, you must use an explicit return statement. 在块体中,必须使用显式返回语句。

The arrow function in your example has the concise body format, so there's an implicit return of the expression ( x.CatalogID == catID ). 示例中的箭头函数具有简洁的正文格式,因此表达式隐式返回( x.CatalogID == catID )。

When you use a function expression (or a block body arrow function), you have to explicitly declare the return: 当您使用函数表达式(或块体箭头函数)时,您必须显式声明返回:

let updateCatInfo = e.sender.dataSource._data.find(function (x) { return x.CatalogID == catID });

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

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