seamless-immutable array property is not immutable

780 Views Asked by At

I wonder if there is a way to convert an object using seamless-immutable where the resultant immutable's array properties are also immutable, currently

a = {hey: [1,2,3], ho: {hi:'there'}}
ia = Immutable(a)
ia // immutable

ia.hey // not immutable
ia.ho //immutable

what I want is ia.hey, an array, being immutable as well, does anyone know how ?

1

There are 1 best solutions below

2
Michael Tontchev On

Unless I'm misunderstanding your question, you can accomplish what you're asking about by using seamless-immutable in development mode. See here:

https://github.com/rtfeldman/seamless-immutable#performance

In the development build, objects are frozen. (Note that Safari is relatively slow to iterate over frozen objects.) The development build also overrides unsupported methods (methods that ordinarily mutate the underlying data structure) to throw helpful exceptions.

As it notes, this comes with a performance penalty (which is actually quite significant, but most likely not relevant in most scenarios).

See this JSFiddle: https://jsfiddle.net/pvqzh9yj/

var array = Immutable(["totally", "immutable", {hammer: "Can’t Touch This"}]);

array[1] = "I'm going to mutate you!";

array[2].hammer = "hm, surely I can mutate this nested object...";

for (var index in array) {  console.log(array[index]); }