JS: Objects
Theory: Objects in use
Let's look at an object usage example. We'll write a function that takes the path to a file as input and returns information about the file as an object.
Node.js has a built-in path module that can quickly retrieve the data you need. That's what you should use when writing real code, but here we're focusing not on how to get the data, but on how to create an object.
First, we need to extract the file name. This can be done using the split() method.
You can do the same thing to get the file extension:
Now, combining everything together, we implement the desired function:
In the preceding example, the object is created as soon as all of the data is ready. When done differently, the object is initialized at the start and gradually filled with data:
Which way do you prefer? In the vast majority of situations, the first way is better. When an object is created with all the data at once, its structure is obvious at a glance. In the second example, you have to run your eyes over the whole code to see what you end up with. On the other hand, the second method may be necessary for situations where the object is filled according to conditions that may not be met:
It's much rarer, but it still happens.
Simplified object creation syntax
When an object is created immediately with all the data filled in, it often looks like the examples above:
Note that the key name and the constant name that contains the value for this key are the same. This is such a common way to create objects that they add a simplified object creation syntax to JavaScript. If the name of the constant matches the property name in the object, you can simply add the constant name to the object definition without specifying the property:
As real life has shown, this approach has proved to be very convenient and practical. In addition, it can also be combined with the usual way to create an object. JavaScript allows you to mix different ways of defining within a single object:

