I am working with another developer's code and have found this construct. The code seems to be instantiating an object within a function's argument list. I would like to read about this. What is the name of this construct? What is its purpose?
function FooBar( { Foo = 'Bar' } = {} ) {
console.info(Foo);
}
FooBar();
FooBar('Baz');
FooBar(Foo = 'Baz');
FooBar({Foo : 'Ah! This is the one!'}); // <--- not like the others
FooBar( { Foo = 'Baz' } = {} );
Here are my thoughts:
{ Foo = 'Bar' }
seems to be creating a new object with a single property.= {}
seems to be assigning a new object to that object? What? Why?- Could we not just set
var Foo = 'Bar'
inside the function body? - Is the construct instead a way of defining a default value for a parameter?
This is the actual code:
import Leaflet from 'leaflet';
export function configure(
frameworkConfig,
{ LeafletDefaultImagePath = 'some/path' } = {}) {
Leaflet.Icon.Default.imagePath = LeafletDefaultImagePath;
frameworkConfig.globalResources('./leaflet.js');
}