News for hackers. Stuff that matters.
Re:Overview of JavaScript ES6 features by neximo64 on Monday October 24, 2016 @09:32AM (#12778582)
Using const and `splice` breaks the rules:

const info = [1,2,3,4]

const newInfo = info.splice(2);

'info' has changed

Re:Overview of JavaScript ES6 features by josephg on Monday October 24, 2016 @09:37AM (#12778614)
The const keyword makes the object reference constant. It doesn't make the object's value constant. You can change the contents of 'info' (info.push(5) is fine). You just can't change which object the variable points to. (info = [] will throw).

If you know C/C++, the code 'const info = []' makes 'info' a constant pointer to a list not a pointer to a constant list.

If you want to stop a variable from being changed, use Object.freeze() - https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...

Re:Overview of JavaScript ES6 features by samcodes on Monday October 24, 2016 @10:27AM (#12779054)
This was what I came to comment on; when introducing ES6 features, this always trips people up. Thanks for "constant pointer to a list, not pointer to a constant list", I will use that next time I explain this.

This also means that objects defined with const can be mutated:

const x = { y: 2 }; x.y = 3;

Doesn't error. However with types that are immutable, like numbers and strings, const really is a constant: const HELLO = "hi" will never change.

Re:Overview of JavaScript ES6 features by Tankenstein on Monday October 24, 2016 @09:40AM (#12778633)
Common misconception, const only disallows reassignment. For instance,

const number = 1337;

number = 10; // fails

But objects/arrays are not immutable in js, so you can do this:

const person = { name: 'Dude' };

person.name = 'Dudette';

Which is perfectly valid. If you want full immutability, i recommend you check out https://facebook.github.io/immutable-js/, been running it in production, a real pleasure to work with.

Re:Overview of JavaScript ES6 features by jasoncchild on Monday October 24, 2016 @09:56AM (#12778780)
FWIW Immutable.js is not deeply immutable. Seamless-immutable however is.
Re:Overview of JavaScript ES6 features by masklinn on Monday October 24, 2016 @10:24AM (#12779029)
> Seamless-immutable however is.

Only in development mode, by freezing the objects.

Re:Overview of JavaScript ES6 features by Roboprog on Monday October 24, 2016 @12:28PM (#12780307)
Any popular "deep freeze" utilities around?
Re:Overview of JavaScript ES6 features by pitaj on Monday October 24, 2016 @02:08PM (#12781236)

    function deepFreeze(obj) {
      const out = Object.freeze(obj);
      Object.keys(out).forEach(key => {
        if (typeof out[key] === 'object') {
          out[key] = Object.freeze(out[key]);
        }
      });
      return out;
    }
Re:Overview of JavaScript ES6 features by Roboprog on Monday October 24, 2016 @08:22PM (#12784128)
I think you meant "out[ key ] = deepFreeze( out[ key ] )" in the innermost step :-)

(recursive)

Re:Overview of JavaScript ES6 features by Cthulhu_ on Monday October 24, 2016 @09:46AM (#12778687)
The only language I've found / used so far that has these (expected) mechanics is Swift, which, when using a `let` for e.g. a list, will make the list itself immutable (in addition to the reference). It's really something that is confusing in every language that has some form of 'final', be it Java (which added immutable list implementations or wrappers to their existing collections), C++, JS, or what-have-you.
Re:Overview of JavaScript ES6 features by masklinn on Monday October 24, 2016 @10:22AM (#12779005)
> The only language I've found / used so far that has these (expected) mechanics is Swift

Rust has similar semantics: https://is.gd/hsRjPh versus https://is.gd/ROgzXl

IIRC you can also declare const objects in C++, but constness is more complex and overridden there.

Re:Overview of JavaScript ES6 features by robrichard on Monday October 24, 2016 @09:37AM (#12778616)
const only guards against changing the reference that it was assigned to. This won't break because info is still assigned to the same array. It doesn't matter that the array was mutated.