Skip to content
Advertisement

Accessing an object property with a $class definition

Let’s have an object:

JavaScript

I want to check the value of timeout from CloneOption.

I unsuccessfully tried things like:

JavaScript

Advertisement

Answer

A way to do it is this:

JavaScript

Because scm.extensions is a List, we use find to take one element out of it that matches the condition inside the closure… in the closure, we ask which element has a property $class whose value is CloneOption.

find returns either one element that matches the condition, or null, so to access timeout we use the null-safe operator: ?.timeout.

You can go further and add a helper method to Maps (the type of your “object”) which lets you access property from it or its “extensions” more easily:

JavaScript

Now, you can use getProp to achieve that:

JavaScript

Which will print:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement