Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

lambda - What exactly does the anonymous JavaScript function f => f do?

I'm using a third-party library that has a function that takes functions as arguments. I'm doing some conditional checks to decide whether or not to add a particular function as a parameter and in some cases I don't want to provide a function. Providing null in that cases throws an error.

I found this code which works, but I don't fully understand what's happening.

compose(__DEV__ ? devTools() : f => f)

Is f => f equivalent to () => {} an empty anonymous function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

f => f is the identity function. It simply returns the argument that was passed in.

This function is often used as a default values for transformation processes, since it doesn't perform any transformation.

Is f => f equivalent to () => {} an empty anonymous function?

No. The empty function doesn't return anything. The identity function returns the passed in argument.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...