8
Because of Ecmascript 6 I see examples that declare literal objects with the reserved word const.
Code example:
// Versão utilizada atualmente
const obj = {
x: 'example'
};
// Versão utilizada anteriormente no EcmaScript 5
var obj = {
x: 'example'
};
Basically, my doubts are as follows::
- This is done to keep the object unchanging?
- It is a protection to prevent the variable from being overwritten improperly by the programmer?
- It is a prevention of the creation of variables and functions with the same name by forgetfulness?
PS: The doubt is not about literal objects, but about this practice of using const to declare them.
Just for the record
constnay makes the object immutable. For this, you must use theObject.freeze.– Luiz Felipe
Complementing the previous comment...
constmakes the variable immutable, but what the variable has is not the object itself, but its reference– Costamilam
Related: https://answall.com/a/206121/112052
– hkotsubo