News for hackers. Stuff that matters.
Re:Immutable JavaScript data structures, backwards-compatibl... by rtfeldman on Monday November 03, 2014 @02:26PM (#8552862)
(Library author here.) Unfortunately, in JavaScript it's not possible to override array element reassignment behavior. You can call Object.freeze on the array, which makes reassignment silently do nothing, but you cannot override it to throw an exception.

The reason the docs say "the methods that would normally mutate..." is that in the specific case of methods (e.g. array.push()), the library can and does override them to throw exceptions.

I would certainly prefer to make that example throw an exception, but it's simply not possible in JS. :)

Re:Immutable JavaScript data structures, backwards-compatibl... by underwater on Monday November 03, 2014 @02:48PM (#8552997)
Object.freeze will throw if you `'use strict';`.
Re:Immutable JavaScript data structures, backwards-compatibl... by rtfeldman on Monday November 03, 2014 @02:53PM (#8553029)
Very true...I should note that in the documentation. Thanks!
Re:Immutable JavaScript data structures, backwards-compatibl... by vjeux on Monday November 03, 2014 @07:19PM (#8554459)
You can define a setter that throws in order to throw without using 'use strict;'. However, additions will still be silent :(

https://gist.github.com/vjeux/6b390e6a6bb51e24b646

    __DEV__ = true;
    var array = ['a', 'b', 'c']
    deepFreezeAndThrowOnMutationInDev(array)
    array[0] = 10;
    Error: You attempted to set the key `0` with the value `10` on an object that is meant to be immutable and has been frozen.
Re:Immutable JavaScript data structures, backwards-compatibl... by arasmussen on Monday November 03, 2014 @04:46PM (#8553732)
Thanks for sharing that tidbit of information. I did not know that and now I do, I appreciate it!

I wonder what the reason behind that is, if you're feeling extra motivated you could even look into having a conversation with the folks who decide what future versions of JS look like to add something like it. I am a huge proponent of immutability and can see how being able to override array element assignment behaviors would improve your library.

Re:Immutable JavaScript data structures, backwards-compatibl... by geocar on Monday November 03, 2014 @04:48PM (#8553751)

    Object.defineProperty(x,'foo',{set:function() { throw new Error(69) }})