4
How do I declare a constant object in Javascript? For example:
/* meu código */
object_teste = {valor:5}
/* console navegador */
object_teste.valor=10;
console.log(object_teste.valor) // aqui ele me retorna 10 ao invés de 5
How to leave this constant value? As if it were a constant variable but in this case an object.
Related questions: "How to declare a constant in javascript?" and "How to create an immutable object in Javascript?" (I’m not sure if any of them are duplicates or if they’re just similar questions, so I’m not voting to close)
– mgibsonbr
The question is good and the answers too, but what do you plan to use the frozen object for? If possible, avoid freezing objects. They get more slow, not to mention that
Object.freeze
does not work in IE8.– bfavaretto
to declare a use variable
var
: var object_test = {value:5}, in case you set the object again, if you want a constant, Object.Freeze(your variable);– Ivan Ferrer