There are native ways of parsing options for methods, or objects in general, however in lodash there is a quick convenience method that can be used to handle this process which is called the _.defaults object method. In this post I will be showing some quick use case examples of the _.defaults method, as well as some vanilla js alternatives when it comes to option parsing and javaScipt methods.
This is a post on the _.defaults method in lodash that can be used to set default values to an object if there is no property there for a given property is a defaults object. This method is then a solution for dealing with options objects when making constructors, of helper methods that accept an object as an argument that contain many properties, as well as with just objects in general actuality. That is that I want a way to fill in the blanks with many object properties when making a constructor function, a helper method that takes an options object, or just about any kind of object in general actually that should have default fall back values if none are given.
A hello world of sorts with lodash defaults can be something as simple as passing a source object that should be a point as the first argument to the lodash defaults, followed by another object that is the default values for a position object. The source object can then have a x value, y value, or neater actually as long as defaults are defined in the object the defines what the defaults should be for x and y.
So then I can pass any object as the first argument of defaults and default x and y values will be set for that object if they are not there to begin with. That is just about it more or less when it comes to this method, it is a way to set default values for any and all values that are not set for a source object. There is maybe just one thing to point out with this though and that is the fact that the defaults method will mutate an object in place, so then this makes the defaults method one of the methods in lodash that are not functional programing friendly then.
The lodash assign method then might prove to be another note worthy option for this sort of thing then. This can also be used to assign properties to a new object in a way in which a given set of values will be used over defaults, else the defaults will be used. Just call the lodash assign method, pass a new object as the first argument, followed by the defaults object, followed by the source object. The returned value will then be a new object with defaults filed in for anything and everything to which there is no given value in the source object, thus it will not mutate in place if used this way.
So one use case example is when making a constructor function, and I want to have an options object that can be passed to the method. This options object contains properties that are to be set as the initial state of an instance of the constructor when used with the new keyword. The _.defaults method can be used along with _.merge to make quick work of this.
So say you have a constructor method that creates a simple Box Class object. You want to make it so that there are default values for the width, height, and x an y properties of the Box Class instance. The lodash defaults method can be used to do just this with an options object that is used for the constructor. Just pass the options object as the first argument to the defaults method, and then as the second argument an object of default values for each property.
This approach works fine as long as all properties given in the options object can be set to the instance of the Constructor, else I might want this options object to be a separate argument, or have other properties that are just used once during construction handled in another object.
This might work fine if I just have a few properties like in this example, but if the constructor gets a little complex, it is nice to have a better solution for handing this.
The lodash _.defaults method one of the many methods in lodash that makes it a great utility library. This is something that comes up a lot when making projects. One way of other I just work through it, but if lodash is part of the stack it is nice to know that I have this available as an option. If you enjoyed this post you might want to check out my main post on lodash in general, or one of my many other various posts on lodash.