Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

You could use const all over the place.


view as:

Still doesn't give you immutable data structures though. For example:

    const myArray = [42];
    myArray.push("Oh noes");

    [42, "Oh noes"]

Ugh, you're kidding?

EDIT: checked in Node, yep. That's unfortunate. Can we get a let foo immutable = [1]; or something?

EDIT2: Although this can be implemented in user-land with Object.freeze. Create a function that returns a const that is frozen if it is non-primitive. That should cover it, right?


freeze/seal don't actually provide true immutability in JS. There are cases where a frozen object can still be modified (Date is one of them, IIRC typed arrays are another)

ClojureScript provides truly immutable data structures that compile down to javascript. You can use them from JS:

https://github.com/swannodette/mori


Legal | privacy