You could manually set the array's mutator methods:
var mutatorMethods = ['fill', 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'];
function preventMutation() {
throw new TypeError('Array is immutable');
}
function makeImmutableArray(origArray) {
mutatorMethods.forEach(function(method) {
origArray[method] = preventMutation;
});
return origArray;
}
var foo = makeImmutableArray(['foo', 'bar', 'baz']);
See this jsbin as an example: http://jsbin.com/zeser/1/edit
This however, only glazes over a bigger problem (considering one could still modify the array through direct indexing foo[0] = 'froboz';): Why do you need an immutable array?
Let me rephrase, who are you trying to prevent from modifying the array? If this is publically facing I would say make your own object and expose the kinds of methods you expect them to use. Adding one level of function calls to iterate is negligible to performance and it prevents exposing the mutator functions.
Lastly if you are protecting mutation from yourself then your coding by extreme paranoia. It adds complexity. Defensive Coding is better served (IMHO) for interactions from outside your system. Defending against your own mistakes only makes the code more complicated because you would be second guessing yourself and often lacking consistency in the plethora of if/else checks.
Instead I'd offer encapsulating the concept of an iterative object by making your own Array like object and using that.
Arrayinstance for the identity matrix once and you have to freeze it once. Sounds like you are looking for a solution requiring a problem.