804Freezing Objects and Arrays in Javascript
Since the ECMA-262 specifications (aka Javascript 1.8.5 aka ECMAScript 5th Edition) it is possible prevent Objects from accepting any changes to their properties. After applying Object.freeze(myObj) it won't be possible to change, add or remove properties from myObj.
So far so good.
As in Javascript Arrays also inherit from Object, I could see not reason, why freezing an array should not work.
var obj = {'a':1, 'b:2'};
Object.freeze(obj);
Object.isFrozen(obj); // returns true
obj.a = 10; // new assignment has no affect
obj.a; // returns 1
var arr = [1, 2];
Object.freeze(arr);
Object.isFrozen(arr); // returns true
arr[0] = 10;
arr; // returns [10, 2] ... ouch!
It turns out, that it seems to be an implementation bug in Safari (5.1). It's working in the latest Firefox and Chrome releases.
To check whether freezing arrays works in your brower, click the following button:
ps. I asked the question first on Stack Overflow: 'Freezing' Arrays in Javascript?. Thanks for the constructive answers.