News for hackers. Stuff that matters.
Re:How necessary are var, let, and const? by pm24601 on Monday June 01, 2015 @11:50AM (#9639566)
const IS valuable and necessary.

1) It means that every place the const appears it can be replaced with the const's value. Type checking can be streamlined, less indirection.

2) It communicates the developer's INTENT in an enforced manner. No later code change can accidentally make the const suddenly non-const.

3) const + deep Object.freeze() makes for a complete const.

Sure a human could hypothetically scan the code to see that the value has not been reassigned. This falls apart in practice. As soon as the company has a few dozen developers the code is constantly changing and such manual analysis would have to be constantly done.

Re:How necessary are var, let, and const? by titzer on Monday June 01, 2015 @11:58AM (#9639631)
1.) is simply wrong. Const variables have a live-range hole where access to the variable either throws or returns undefined, depending on the language mode (and the VM!). In either case it not legal to just smash in the constant.

2.) That's nice but JS semantics do not actually hold up. The intent is for a constant, the reality is there is a small window where the wrong results can be observed. Engines don't actually do any more optimizations than for var or let; in fact, your code may actually be slower due to the live-range hole checks.

Re:How necessary are var, let, and const? by pm24601 on Monday June 01, 2015 @12:07PM (#9639738)
So you are saying that there are race conditions currently? Are any of this race conditions actually required by the language spec? If not, I suspect that those race conditions will be eliminated.
Re:How necessary are var, let, and const? by david-given on Monday June 01, 2015 @12:27PM (#9639904)
I believe that what the parent meant was that this:

function f() { console.log(a); const a = 1; console.log(a); }

...actually returns this:

undefined 1

So you get partially compile-time semantics (you can't assign to a) but partially run-time semantics (a only gets its value when initialised). Simply propagating the value of a's initialiser to all places where the variable is used is wrong, as this behaviour's required by the spec.

Re:How necessary are var, let, and const? by AgentME on Monday June 01, 2015 @03:51PM (#9641635)
Actually that code example should throw an exception when the first `a` is evaluated.
Re:How necessary are var, let, and const? by nullz on Monday June 01, 2015 @04:28PM (#9641906)
It doesn't in v8 -- what do you mean by `should`?
Re:How necessary are var, let, and const? by egeozcan on Monday June 01, 2015 @04:54PM (#9642083)
It's an implementation error. In Firefox, it correctly throws:

> ReferenceError: can't access lexical declaration `a' before initialization

Re:How necessary are var, let, and const? by titzer on Tuesday June 02, 2015 @04:51AM (#9644653)
No, legacy const is not spec'd by ES5. It behaves differently in FireFox and V8 because it was added at different times with different semantics in the live range hole case. In strict mode, it will throw on V8 as well.
Re:How necessary are var, let, and const? by Skinney on Monday June 01, 2015 @04:54PM (#9642077)
No it shouldn't. `a` at the first console.log invocation IS undefined, so it is entirely correct.
Re:How necessary are var, let, and const? by anamexis on Monday June 01, 2015 @07:33PM (#9642929)
Not according to the spec[1]:

> The variables are created when their containing Lexical Environment is instantiated but may not be accessed in any way until the variable’s LexicalBinding is evaluated.

I.e. given a variable declared with `const`, you may not access it before it is set.

[1] http://people.mozilla.org/~jorendorff/es6-draft.html#sec-let...

Re:How necessary are var, let, and const? by rescendent on Friday June 05, 2015 @03:35PM (#9667605)
Consts have other purposes: Speeding up global constants using fixed field optimization https://blogs.windows.com/msedgedev/2015/05/20/delivering-fa...
Re:How necessary are var, let, and const? by braythwayt on Monday June 01, 2015 @12:02PM (#9639674)
> 3) const + deep Object.freeze() makes for a complete const.

The trouble with this approach is that runtime checks are only a guarantee that the code you've _exercised_ does not break the contract.

In that respect, `const` is deeply different than `.freeze`, in the same way that runtime assertions are deeply different than type checking.

Re:How necessary are var, let, and const? by pm24601 on Monday June 01, 2015 @12:05PM (#9639710)
Huh? That's why I pointed out the joint usage. And either way, a instant failure is better than a silent change.
Re:How necessary are var, let, and const? by jonny_eh on Monday June 01, 2015 @04:07PM (#9641759)
Aren't points 1 and 2 just arguments for a stricly typed language?
Re:How necessary are var, let, and const? by pm24601 on Monday June 01, 2015 @04:34PM (#9641946)
Not at all. Simple things like instanceof / typeof checks can be resolved without a static type system. Because with a const everything is known about the const in question. Obviously the const prototype may change but then again if the const has a frozen prototype - that too can have static analysis applied to it.

I am simply repeating the well-known benefits to immutables which is completely orthogonal to a type system, static or otherwise.

Re:How necessary are var, let, and const? by Havvy on Tuesday June 09, 2015 @02:59PM (#9687879)
instanceof cannot be determined statically because I can do...

    const a = {};
    Object.setPrototypeOf(a, SomethingElse);
Re:How necessary are var, let, and const? by pm24601 on Thursday June 18, 2015 @03:49AM (#9737278)
really? even on a frozen object?

Remember I have said const PLUS Object.freeze() Both are needed to get the true immutable effect.

Re:How necessary are var, let, and const? by catshirt on Monday June 01, 2015 @01:13PM (#9640332)
isn't 2 kind of a misdirected optimization for an untyped language?
Re:How necessary are var, let, and const? by pm24601 on Monday June 01, 2015 @04:38PM (#9641974)
How is INTENT of immutability ( and with const - now an enforced immutability ) misdirected optimization in any language.

Javascript is dynamically weakly typed but it is still typed, typeof/instanceof exist for a reason.

Re:How necessary are var, let, and const? by catshirt on Monday June 01, 2015 @05:11PM (#9642181)
sorry right- weakly typed.

my point is if you are using a weakly typed language, it doesn't care much about developer intent with respect to type. so why would it be concerned with developer intent with respect to mutability? seems arbitrary.

Re:How necessary are var, let, and const? by pm24601 on Monday June 01, 2015 @10:31PM (#9643482)
maybe because developers in general prefer to not have bugs?