Named object parameters in ES6 - How do I check they are provided?

Viewed 178

I have some ES6 code where I am passing in some named parameters of a defined options object like so...

    configureMapManager({ mapLocation, configuredWithDataCallback, locationHasChanged })
{
    if (mapLocation) console.log(mapLocation)
}

This is a contrived case and the following calls will work fine...

configureMapManager({ mapLocation: "BS1 3TQ" })
configureMapManager({})

But this will blow up...

configureMapManager()

...because I cant check that the passed in object is defined (It's not because I called the method without any parameters). How can I do this without having to rewrite this like this (which sucks because then you lose visibility of the allowed parameters within the object)...

    configureMapManager(options)
{
    if (options && options.mapLocation) console.log(mapLocation)
}
1 Answers
Related