Chris
Chris

Reputation: 3129

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. 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

Upvotes: 1

Views: 231

Answers (1)

Daniel
Daniel

Reputation: 11172

According to the docs regarding Arrow Functions > Function body:

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).

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 });

Upvotes: 3

Related Questions