Great one-line helper!
I think the post misses the the motivation behind navigator function. Indeed, why use navigator(test, 'a.b.c') instead of short test.a.b.c?
This navigator function helps to avoid runtime errors when trying to read properties from null or undefined. test.z.x will throw, but navigator(test, 'z.x') will return undefined.
This is such a common problem, so TC39 proposes optional chaining with syntax test?.a?.b?.c which is currently in stage2.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great one-line helper!
I think the post misses the the motivation behind
navigator
function. Indeed, why usenavigator(test, 'a.b.c')
instead of shorttest.a.b.c
?This
navigator
function helps to avoid runtime errors when trying to read properties fromnull
orundefined
.test.z.x
will throw, butnavigator(test, 'z.x')
will returnundefined
.This is such a common problem, so TC39 proposes optional chaining with syntax
test?.a?.b?.c
which is currently in stage2.