Live data from Hacker News

Overview of JavaScript ES6 features

adrianmejia.com

1–10 of 250 posts

Re: Overview of JavaScript ES6 features

#3
post #2

Using const and `splice` breaks the rules: const info = [1,2,3,4] const newInfo = info.splice(2); 'info' has changed

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

#4
post #2

Using const and `splice` breaks the rules: const info = [1,2,3,4] const newInfo = info.splice(2); 'info' has changed

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.

Re: Overview of JavaScript ES6 features

#5
post #2

Using const and `splice` breaks the rules: const info = [1,2,3,4] const newInfo = info.splice(2); 'info' has changed

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

#6
destructing is very useful and encourage good coding styles

Is it? Personally I'd say that was bad code. What so wrong with using the original objects?

Putting aside the need to variable swap once a year or so, all the other examples look really confusing to me and unclear what they're doing. The `Deep Matching` especially.

Re: Overview of JavaScript ES6 features

#7
On the web browser side, I don't recommend using ES6 yet, without any kind of fallback. Internet Explorer 11 is still used, as are devices on older iOS versions. (without counting people using the default browser on pre-Lollipop Android)

Re: Overview of JavaScript ES6 features

#10
post #2

Using const and `splice` breaks the rules: const info = [1,2,3,4] const newInfo = info.splice(2); 'info' has changed

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.
Post reply on HN