Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Freezing' Arrays in Javascript?

Since the ECMA-262 specifications Javascript has gained the Object.freeze() method, which allows for objects, whose properties can not be changed, added or removed.

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 

So far so good.

I am wondering, whether freeze() should also work on Arrays.

var arr = [1, 2]; Object.freeze(arr); Object.isFrozen(arr);      // returns true arr[0] = 10; arr;                       // returns [10, 2] ... ouch! 

Maybe I am wrong, but I was under the impression, that Array inherits from Object.

typeof obj                 // "object" typeof arr                 // "object" 

Any ideas, pointers, enlightenments would be highly appreciated.

like image 525
trembl Avatar asked Sep 22 '11 05:09

trembl


People also ask

What is frozen array?

Frozen arrays can be safely shared without coordination or risk of unexpected modification. Freezing is a more efficient alternative to defensive copying, in that the copy can frequently be optimized away by the runtime.

What is a freeze method in JavaScript?

The JavaScript Object. freeze() method freezes an object. A frozen object can no longer be changed. Freezing an object prevents: New properties from being added to the object.

How do you unfreeze an array in JavaScript?

You can unfreeze an array by using spread operator. That just creates a copy. It does not unfreeze it.

What is a freeze method?

freezing, in food processing, method of preserving food by lowering the temperature to inhibit microorganism growth. The method has been used for centuries in cold regions, and a patent was issued in Britain as early as 1842 for freezing food by immersion in an ice and salt brine.


2 Answers

Yes, freeze should work for Arrays, the behavior you are experiencing is clearly an implementation bug.

This bug might be related to the fact that array objects implement a custom [[DefineOwnProperty]] internal method (the magic that makes the length property work).

I just tested it on two implementations and it works properly (Chrome 16.0.888, and Firefox Aurora 8.02a).

About your second question, well, array objects inherit from Array.prototype which inherits from Object.prototype, for example, you can access non shadowed methods from Object.prototype directly on array objects:

['a'].hasOwnProperty('0'); // true 

But this isn't related about how the typeof works, this operator will return 'object' for any object intance, regardless its kind, and for the null value, which people has always complained about.

The rest of possible return values of the typeof operator, correspond to the primitive types of the language, Number, String, Boolean, Symbol and Undefined.

like image 66
Christian C. Salvadó Avatar answered Oct 11 '22 06:10

Christian C. Salvadó


Yes, it is applicable to arrays too.

const arr = [1,2,3,4]; Object.freeze(arr); Object.isFrozen(arr)// true arr.push(5) // you will get a type error arr.pop() // you will get a type error 
like image 37
Meghna Khound Avatar answered Oct 11 '22 04:10

Meghna Khound