Safely Access Object Properties with `prop`

InstructorAndy Van Slaars

Share this video with your friends

Send Tweet

In this lesson, we’ll use a Maybe to safely operate on properties of an object that could be undefined. We’ll use our initial code as the basis for a prop utility function that can be reused with different objects and various property names. Instead of just blindly asking for a property, this version of prop will drop us into the safe confines of a Maybe, giving us a Just when the property exists and a Nothing for an undefined property. Once we’ve built up our own prop utility, we’ll refactor the code one more time to take advantage of the built-in prop utility provided by the crocks library.

Eleni Lixourioti
~ 6 years ago

Is there any difference between ramda's compose and crocks' compose that we saw in the previous lesson, or can they be used interchangeably? Is there a reason to prefer one over the other?

Many thanks, this is all very interesting and well explained!

Andy Van Slaarsinstructor
~ 6 years ago

Nope, they both accomplish the same goal and you should be fine using either one. I would try to be consistent with which library I use for a particular function throughout a project, but either choice should be fine if you're already pulling in both libraries.

Glad you're enjoying it so far!

Simon Nonnis
~ 6 years ago

Very good job Andy. How would you handle the case when the value of the key you are interested in is not an integer but a string?

Germán Küber
~ 5 years ago

which font is he using? Somebody know it?