0

Hello and thank you in advanced.

I recently read about object.freez and deep.freez

And considering that js has no immutable structures, I'm now left wondering how this differs from the standert term of immutability...

"quote: As said before JavaScript has no immutable structures, but immutability can be achieved by principles and rules."

how ever seeing: code from :source

    function deepFreeze(object) {

    // Retrieve the property names defined on object
    var propNames = Object.getOwnPropertyNames(object);

    // Freeze properties before freezing self

     for (let name of propNames) {
       let value = object[name];

      object[name] = value && typeof value === "object" ? 
      deepFreeze(value) : value;
    }

    return Object.freeze(object);
  }
  var obj2 = {
    internal: {
    a: null
    }
  };

  deepFreeze(obj2);

  obj2.internal.a = 'anotherValue'; // fails silently in non-strict mode
  obj2.internal.a; // null

I'm now left at a bit perplexed

I thought immutability meant not being able to mutate(change) the values of an object after it has bean created. And as far as i can tell deep.freez achieves exactly that... So what is the difference?

hope this question made sense as i was not able to find any information concerning deep.freez

Kind regards Lost.

  • 1
    The quote is incorrect, and has been [since 2009](http://ecma-international.org/ecma-262/5.1/#sec-15.2.3.9) (that link is to 5.1, but `Object.freeze` was in [5.0](http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262%205th%20edition%20December%202009.pdf) [pdf] in 2009, it's just not available officially as linkable HTML). – T.J. Crowder Jun 15 '18 at 13:15

1 Answers1

4

Both points are correct - there are no data structures in JavaScript that are immutable by default. For example, there is no data structure you can create which is an "immutable object" at the time of creation.

However, Object.freeze is a method in the JavaScript/ECMAScript specification which freezes the properties of an object after it's been created, essentially making it immutable.

Your deepFreeze function, at its heart, calls Object.freeze. deepFreeze ensures that if an object has a property which is also an object, it will also be frozen.

The article you linked to actually mentions Object.freeze as a method of creating "immutable" objects.

ttarik
  • 3,824
  • 1
  • 32
  • 51
  • 1
    @LostDemondragon I've updated my answer to provide more context. There is no functional difference, but the article you linked is technically correct in that there are no data structures in JS that are immutable *by default*. Once you call `Object.freeze`, though, [it can't be unfrozen](https://stackoverflow.com/a/19293418/400966), so for all intents and purposes it is immutable. – ttarik Jun 15 '18 at 13:17
  • 1
    @LostDemondragon - The quote is just incorrect. A frozen object whose property values are all read-only primitives or read-only references to frozen objects is immutable. JavaScript isn't designed ground-up for immutability, but it does have it, as of 2009. And the 2015 and upcoming 2018 specs provide further tools for better immutability-focussed programming, though it's still not (and probably never will be) JavaScript's default mode. – T.J. Crowder Jun 15 '18 at 13:18
  • strings are immutable by default. – ASDFGerte Jun 15 '18 at 13:19
  • @ttarik ahhh "there are no data structures in JavaScript that are immutable by default."... thats what i needed... Thank you very much :3 i will except you're answer... have a wonderful day. Thank you so much. – Lost Demondragon Jun 15 '18 at 13:31