According to the documentation of Object.freeze
, there is no ready method for this, it is necessary to create a special function to iterate on the fields of the object and apply freeze
in each of them:
function deepFreeze (o) {
var prop, propKey;
Object.freeze(o); // Primeiro congela o objeto.
for (propKey in o) {
prop = o[propKey];
if (!o.hasOwnProperty(propKey) || !(typeof prop === "object") || Object.isFrozen(prop)) {
// Se o objeto está no prototype, não é um objeto, ou já está congelado, pule.
// Note que isso pode deixar uma referência não congelada em algum lugar no objeto
// se já existe um objeto congelado contendo outro objeto não congelado.
continue;
}
deepFreeze(prop); // Chama deepFreeze recursivamente.
}
}
Example of the special case mentioned in the code (to avoid it, comment the part where it checks if the object is already frozen to not freeze it again):
var raso = { foo:{ bar:"baz" } };
Object.freeze(raso); // { bar:"baz" } continua não congelado
var fundo = { raso:raso }
deepFreeze(fundo); // raso não é afetado, pois já estava congelado
fundo.raso.foo.bar = 42; // Atribui corretamente
However, special care should be taken with cases where the object has circular references. This is not a problem in the above code as it is (since checking for "already frozen" prevents the same object from being visited twice - and therefore avoids an infinite loop), but becomes a problem if this test is removed.
Finally, it is worth mentioning that depending on the implementation freezing an object can negatively impact performance (as cited in a comment to a related question) - contrary to what would normally be expected, that this immutability would bring the possibility of optimizations that a mutable object does not allow.
Perfect to have created another question and answered it instead of leaving it lost in the constant variable answer at http://answall.com/q/6190/2884. With this answer here will make it easier for those who use Google later
– Emerson Rocha
+1 well cool, the answer is the same as I would, but I hope that few people go using this resource, after all I already have my reservations regarding use of constants, imagine now explain to someone who does not understand the scope that he gave a Freeze on an object that is being used and needs to be manipulated in another context but belonging to the same scope.
– Gabriel Gartz