I am seeing a piece of code:
import _invoke from 'lodash/invoke'
const format = _invoke(Intl, 'DateTimeFormat')
And for me it looks like it can be replaced by a simpler variant:
const format = Intl.DateTimeFormat()
The most obvious reason I see is _invoke
won't throw an error if DateTimeFormat
is not available, e.g. undefined
.
However, I am afraid it can work in a different way when changed to latter because it's not just a function but a method of an object?
So, why the author of the code could've used it that way?
The expression indeed looks functionally identical to
Intl?.DateTimeFormat?.()
The author may have wanted to use lodash because they considered it more "idiomatic" in some way, or they may have been developing for a browser version that doesn't support nullish coalescing operators and wanted to avoid constructs like Intl && Intl.DateTimeFormat && Intl.DateTimeFormat()
etc.
As @deceze put it, only the author knows.